program a228653; { Compiles in Free Pascal 2.4.0 or later. Requires GNU Multiple Precision Arithmetic Library to be installed. } uses Crt, Gmp; const max = 225; var b, tot, tri: array [1..max] of longword; fact: array [1..max] of mpz_t; n, k: longword; h, i, j, m, p, q, s: longword; x, y, z: mpz_t; procedure chksol; begin h:=0; mpz_set_ui(x, 1); repeat inc(h); mpz_mul_ui(x, x, b[h]); until h = i; m := 0; mpz_set_ui(z, 0); repeat inc(m); if b[m] >= h then begin q := b[m]; p := 0; mpz_set_ui(y, 1); repeat mpz_mul_ui(y, y, q); dec(q); inc(p); until p = h; mpz_fdiv_q(y, y, fact[h]); mpz_add(z, z, y); end; until (m=h) or (b[m] < h); if mpz_cmp(x, z) = 0 then begin inc(n); inc(s); writeln(n, ' ', k); end; end; procedure nextstep; begin inc(i); tot[i] := tot[i - 1]; repeat inc(b[i]); inc(tot[i]); inc(j); if b[i] < b[i - 1] then begin if (j < k) then begin if ((i + 1) <= b[1]) and (tri[b[i]] + tot[i - 1] >= k) then nextstep; end else chksol; end; until (b[i] = b[i - 1]) or (j = k) or (s > 0); dec(j, b[i]); b[i] := 0; tot[i] := 0; dec(i); end; begin for i := 1 to max do begin mpz_init(fact[i]); mpz_fac_ui(fact[i], i); end; mpz_init(x); mpz_init(y); mpz_init(z); for i := 1 to max do tri[i] := ((i * i) + i) div 2; n := 0; k := 0; repeat inc(k); write(k); gotoxy(1,wherey); s := 0; for i :=1 to k do begin b[i] := 0; tot[i] := 0; end; i := 1; j := 0; repeat inc(b[i]); inc(tot[i]); inc(j); if (j < k) and (tri[b[i]] >= k) then nextstep; until (b[i] = k) or (s > 0); b[i] := 0; j := 0; until (k = max) or keypressed; clreol; for i := 1 to max do mpz_clear(fact[i]); mpz_clear(x); mpz_clear(y); mpz_clear(z); end.