fork download
  1. program enigmath;
  2. Uses Math;
  3. const MAXX=108;
  4. var
  5. Emin, Emax, E, i, S, K, t, sum, a : longint;
  6. cod, dec :array[1..1000000] of Longint;
  7.  
  8. function codifica (x:Longint):Longint;
  9. begin
  10. if x < 10 then codifica:=x (* single digit*)
  11. else
  12. begin
  13. S := 0; K:= x;
  14. while K>0 do
  15. begin
  16. S:=S + K mod 10; (*sum of digits*)
  17. K:=K div 10;
  18. end;
  19. codifica:=x + codifica(S);
  20. end;
  21. end;
  22.  
  23. begin
  24. {
  25.   uncomment the following lines if you want to read/write from files
  26.   assign(input, 'input.txt'); reset(input);
  27.   assign(output, 'output.txt'); rewrite(output);
  28. }
  29.  
  30. for i:=0 to MAXX do
  31. begin
  32. cod[i]:=codifica(i); (* precompute encoding of small numbers*)
  33. dec[i] := -1; (*no decoding found so far*)
  34. end;
  35. for i:=1 to MAXX do write(cod[i],' '); end.
  36. (*sum of digits of the number before the start*)
  37. sum := 0;
  38. if (Emin > MAXX) then
  39. for t:=Emin-MAXX-1 downto 1 do
  40. begin
  41. t:=t div 10;
  42. sum:= sum + (t mod 10);
  43. end
  44. else sum := -1;
  45. a:= max(Emin-MAXX,0);
  46. for E:=a to Emax do
  47. begin (*update sum of digits*)
  48. inc(sum);
  49. for t:=E downto 1 do
  50. begin
  51. if t mod 10 =0 then t:=t div 10;
  52. sum :=sum-9;
  53. end;
  54. (*store encoding of E*)
  55. if E >= 10 then E:=E+ cod[E mod MAXX];
  56. if dec[E mod MAXX] = -1 then E:=E
  57. else dec[E mod MAXX]:=-2;
  58. (* print decodings of E if appropriate*)
  59. if (E >= Emin) then
  60. if dec[E mod MAXX] = -1 then writeln ('IMPOSSIBLE')
  61. else if (dec[E mod MAXX] = -2 then writeln ('AMBIGUOUS')
  62. else writeln (dec[E mod MAXX]);
  63. end;
  64. (* reset*)
  65. dec[E mod MAXX] := -1;
  66. end;
  67. end.
  68.  
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 8 9 11 13 15 17 19 21 23 25 27 30 22 24 26 28 30 32 34 36 39 42 33 35 37 39 41 43 45 48 51 54 44 46 48 50 52 54 57 60 63 66 55 57 59 61 63 66 69 72 75 78 66 68 70 72 75 78 81 84 87 90 77 79 81 84 87 90 93 96 99 102 88 90 93 96 99 102 105 108 111 114 99 102 105 108 111 114 117 120 123 126 101 103 105 107 109 111 113 115 117