Project Euler Problem 498

For positive integers n and m, we define two polynomials Fn(x) = x^n and Gm(x) = (x-1)^m.

Project Euler Problem 498

Solution

Answer: 472294837

Let

$$F_n(x)=x^n,\qquad G_m(x)=(x-1)^m.$$

We define $R_{n,m}(x)$ to be the remainder when $x^n$ is divided by $(x-1)^m$. Since the divisor has degree $m$, the remainder has degree at most $m-1$.

We seek the absolute value of the coefficient of $x^d$ in $R_{n,m}(x)$:

$$C(n,m,d).$$

We are asked to compute

$$C(10^{13},10^{12},10^4)\pmod{999999937}.$$


1. Mathematical analysis

We work around the point $x=1$.

Define

$$y=x-1 \quad\Longrightarrow\quad x=1+y.$$

Then

$$x^n=(1+y)^n.$$

Modulo $y^m$, only the first $m$ terms of the binomial expansion survive:

$$R_{n,m}(x) = \sum_{k=0}^{m-1}\binom{n}{k}(x-1)^k.$$

So the coefficient of $x^d$ is

$$a_d = \sum_{k=d}^{m-1}\binom{n}{k}x^d^k.$$

Now expand:

$$(x-1)^k = \sum_{j=0}^k \binom{k}{j}x^j(-1)^{k-j}.$$

Hence

$$x^d^k = (-1)^{k-d}\binom{k}{d}.$$

Therefore

$$a_d = \sum_{k=d}^{m-1} (-1)^{k-d}\binom{n}{k}\binom{k}{d}.$$


Simplifying the sum

Use the identity

$$\binom{n}{k}\binom{k}{d} = \binom{n}{d}\binom{n-d}{k-d}.$$

Let $j=k-d$. Then

$$a_d = \binom{n}{d} \sum_{j=0}^{m-1-d} (-1)^j \binom{n-d}{j}.$$

Now apply the alternating binomial identity

$$\sum_{j=0}^{r}(-1)^j\binom{N}{j} = (-1)^r\binom{N-1}{r}.$$

With

$$N=n-d,\qquad r=m-1-d,$$

we get

$$a_d = (-1)^{m-1-d} \binom{n}{d} \binom{n-d-1}{m-d-1}.$$

Since $C(n,m,d)$ is the absolute value,

$$\boxed{ C(n,m,d) = \binom{n}{d} \binom{n-d-1}{m-d-1}. }$$


Check against the examples

Example 1

$$C(6,3,1) = \binom61\binom41 = 6\cdot4 = 24.$$

Correct.


Example 2

$$C(100,10,4) = \binom{100}{4}\binom{95}{5}.$$

This equals

$$227197811615775,$$

matching the problem statement.


2. Modular computation

We need

$$\binom{10^{13}}{10^4} \binom{10^{13}-10^4-1}{10^{12}-10^4-1} \pmod p,$$

where

$$p=999999937.$$

Since $p$ is prime, we use Lucas' theorem.


Lucas decomposition

Write numbers in base $p$.

First factor

$$10^{13}=10000\cdot p + 630000,$$

so

$$\binom{10^{13}}{10^4} \equiv \binom{10000}{0} \binom{630000}{10000} \pmod p.$$

Thus

$$\binom{10^{13}}{10^4} \equiv \binom{630000}{10000}.$$


Second factor

$$10^{13}-10^4-1 = 10000\cdot p + 619999,$$

and

$$10^{12}-10^4-1 = 1000\cdot p + 52999.$$

Hence

$$\binom{10^{13}-10^4-1}{10^{12}-10^4-1} \equiv \binom{10000}{1000} \binom{619999}{52999} \pmod p.$$

Therefore

$$C \equiv \binom{630000}{10000} \binom{10000}{1000} \binom{619999}{52999} \pmod p.$$

These are all ordinary combinations below $p$, computable with factorials and Fermat inverses.


3. Python implementation

MOD = 999999937

def mod_comb(n, r, mod):
    """Compute C(n,r) mod prime mod."""
    if r < 0 or r > n:
        return 0

    r = min(r, n - r)

    numerator = 1
    denominator = 1

    for i in range(1, r + 1):
        numerator = (numerator * (n - r + i)) % mod
        denominator = (denominator * i) % mod

    return numerator * pow(denominator, mod - 2, mod) % mod

# Problem values
n = 10**13
m = 10**12
d = 10**4

# Lucas reductions already derived mathematically
a = mod_comb(630000, 10000, MOD)
b = mod_comb(10000, 1000, MOD)
c = mod_comb(619999, 52999, MOD)

answer = (a * b % MOD) * c % MOD

print(answer)

4. Code walkthrough

mod_comb

This computes

$$\binom{n}{r}\pmod p$$

using

$$\binom{n}{r} = \frac{n(n-1)\cdots(n-r+1)}{r!}.$$

Because $p$ is prime, we divide modulo $p$ using Fermat’s theorem:

$$a^{-1}\equiv a^{p-2}\pmod p.$$


Main computation

We use the Lucas reductions derived above:

a = mod_comb(630000, 10000, MOD)
b = mod_comb(10000, 1000, MOD)
c = mod_comb(619999, 52999, MOD)

Then

answer = (a * b % MOD) * c % MOD

gives the desired value.


5. Final verification

  • The derived closed form reproduces both examples exactly.
  • Lucas’ theorem is applicable because the modulus is prime.
  • All reduced binomial parameters are strictly below the modulus.
  • The final residue is in the valid range $[0,p-1]$.

Thus the computation is consistent.

Answer: 472294837