- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 141 lines of Turing from the credited upstream file ccc97s5.t.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123456 789 10var infile : string := "div.in"11var outfile : string := "div.out"12var fi, fo : int13var n, ndvd, ndvs, nquo : int14var dvd : array 1 .. 100 of int15var dvs : array 1 .. 100 of int16var quo : array 1 .. 100 of int17var t : array 1 .. 100 of int18var offset, k, m : int19var can : boolean20var line : string21var key : string (1)22 23open : fi, infile, get24open : fo, outfile, put25 26get : fi, n27for i : 1 .. n28 29 30 get : fi, line31 for j : 1 .. length (line)32 dvd (j) := strint (line (j))33 end for34 ndvd := length (line)35 get : fi, line36 for j : 1 .. length (line)37 dvs (j) := strint (line (j))38 end for39 40 41 ndvs := length (line)42 nquo := 043 offset := 044 loop45 exit when ndvs > ndvd - offset46 nquo := nquo + 147 quo (nquo) := 048 49 loop50 51 for j : 1 .. ndvd52 t (j) := dvd (j)53 end for54 55 56 can := true57 k := ndvs58 m := ndvs + offset59 loop60 exit when m = 061 if k >= 1 then62 if dvd (m) >= dvs (k) then63 dvd (m) := dvd (m) - dvs (k)64 else65 dvd (m) := (dvd (m) + 10) - dvs (k)66 if m = 1 then67 can := false68 else69 dvd (m - 1) := dvd (m - 1) - 170 end if71 end if72 else73 if dvd (m) >= 0 then74 dvd (m) := dvd (m)75 else76 dvd (m) := (dvd (m) + 10)77 if m = 1 then78 can := false79 else80 dvd (m - 1) := dvd (m - 1) - 181 end if82 end if83 end if84 k := k - 185 m := m - 186 end loop87 if can then88 quo (nquo) := quo (nquo) + 189 else90 91 for j : 1 .. ndvd92 dvd (j) := t (j)93 end for94 end if95 exit when can = false96 end loop97 offset := offset + 198 end loop99 100 101 if nquo = 0 then102 put : fo, "0"103 put "0"104 else105 k := 1106 loop107 exit when k >= nquo or quo (k) not= 0108 k := k + 1109 end loop110 loop111 exit when k > nquo112 put : fo, quo (k) ..113 put quo (k) ..114 k := k + 1115 end loop116 put : fo, ""117 put ""118 end if119 120 121 k := 1122 loop123 exit when k >= ndvd or dvd (k) not= 0124 k := k + 1125 end loop126 loop127 exit when k > ndvd128 put : fo, dvd (k) ..129 put dvd (k) ..130 k := k + 1131 end loop132 put : fo, ""133 put : fo, ""134 put ""135 put ""136end for137 138close : fi139close : fo140 141