# Factorial Recovery — Solution Credits: the problem setter’s solution recovers the missing prefix with one sufficiently large modulus. Codex (GPT-6) uses two moduli and the Chinese remainder theorem for exact integer reconstruction. ## Comparing solutions ### Problem setter’s solution: one sufficiently large modulus Let the suffix have length $k$ and integer value $s$ , and let the missing prefix be $x$ . Then $n!=x10^k+s$ and $0\le x<10^{15}$ . The problem setter’s method uses the prime $M=7\cdot2^{52}+1=31525197391593473$ , computes the factorial and suffix residues, and obtains $$x\equiv(n!-s)(10^k)^{-1}\pmod M$$ The inverse exists because $M$ is coprime to $10$ . Since $x10^{15}$ . For each modulus $t\in\{p,q\}$ , multiply the integers up to $n$ to obtain $n!\bmod t$ . Scan the suffix digits using “remainder times ten plus the new digit” to obtain $s\bmod t$ . Compute $10^k\bmod t$ by binary exponentiation and its inverse by Fermat's little theorem. Then $$r_t=(n!-s)(10^k)^{-1}\bmod t$$ Normalize residues to $0,\ldots,t-1$ . Leading zeros do not affect a residue, but must remain in the string used for output. ## Combine the residues Write $x=r_p+pu$ and impose its residue modulo $q$ : $$u=(r_q-r_p)p^{-1}\bmod q$$ Taking $0\le u0$ , print it followed by the unchanged suffix. If $x=0$ , print only the suffix: in valid input, it already contains the complete factorial, without extra leading zeros. When $k=0$ , the suffix is empty and $s=0$ , so print the reconstructed factorial itself. The product starts at $1$ , handling both $0!$ and $1!$ . ## Implementation and complexity Let $P=\max(p,q)$ . With machine-word arithmetic for these moduli, time is $O(n+k+\log(k+1)+\log P)$ and space is $O(k+1)$ . The suffix is stored for output; the reference solution never constructs the large factorial as an integer. Every modular product is less than $P^2<2^{63}$ , and the combined value is less than $pq<2^{63}$ . Standard signed 64-bit C++17 integers suffice, without floating-point approximations or compiler extensions. ## Validation Small cases compare the complete output with Python's exact integer factorial. Formal cases also use exact full-output comparison. Tests cover $k=0$ , $k=D$ , a full fifteen-digit prefix, leading zeros and maximum $n$ . See [environment and measurements](../../../docs/评测环境与性能.md) for performance measurements.