program a228652; { Compiles in Free Pascal 2.4.0 or higher. Requires GNU Multiple Precision Arithmetic Library to be installed. } uses Crt, Gmp; const max = 100; var b: array [1..max] of longword; fact: array [1..max] of mpz_t; k, n: longword; i, j, h, m, p, q, r, 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 inc(s); end; procedure nextstep; begin inc(i); repeat inc(b[i]); inc(j); if (j < k) then begin if (i + 1 <= b[1]) and (j + (b[i] * (b[1] - i)) >= k) then nextstep; end else chksol; until (b[i] = b[i - 1]) or (j = k) or (s > 0); dec(j, b[i]); b[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); n := 0; k := 0; repeat inc(k); s := 0; write(k); gotoxy(1, wherey); for i := 1 to k do b[i] := 0; i := 1; j := 0; repeat inc(b[i]); inc(j); if (j < k) and (b[i] * b[i] >= k) then nextstep; until (b[i] = k) or (s > 0); if s = 0 then begin inc(n); writeln(n, ' ', k); end; 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.