All Euler problems
Project Euler

Remainder of Polynomial Division

For positive integers n and m, we define: F_n(x) = x^n G_m(x) = (x-1)^m Let R_(n,m)(x) be the remainder when dividing F_n(x) by G_m(x). Define C(n, m, d) as the absolute value of the coefficient of...

Source sync Apr 19, 2026
Problem #0498
Level Level 20
Solved By 614
Languages C++, Python
Answer 472294837
Length 296 words
modular_arithmeticalgebranumber_theory

Problem Statement

This archive keeps the full statement, math, and original media on the page.

For positive integers \(n\) and \(m\), we define two polynomials \(F_n(x) = x^n\) and \(G_m(x) = (x-1)^m\).

We also define a polynomial \(R_{n,m}(x)\) as the remainder of the division of \(F_n(x)\) by \(G_m(x)\).

For example, \(R_{6,3}(x) = 15x^2 - 24x + 10\).

Let \(C(n, m, d)\) be the absolute value of the coefficient of the \(d\)-th degree term of \(R_{n,m}(x)\).

We can verify that \(C(6, 3, 1) = 24\) and \(C(100, 10, 4) = 227197811615775\).

Find \(C(10^{13}, 10^{12}, 10^4) \bmod 999999937\).

Problem 498: Remainder of Polynomial Division

Mathematical Analysis

Change of Variables

Let t=x1t = x - 1, so x=t+1x = t + 1. Then:

  • Fn(x)=(t+1)n=i=0n(ni)tiF_n(x) = (t+1)^n = \sum_{i=0}^{n} \binom{n}{i} t^i
  • Gm(x)=tmG_m(x) = t^m

The remainder of dividing (t+1)n(t+1)^n by tmt^m is simply the terms of degree <m< m:

Rn,m(x)=i=0m1(ni)ti=i=0m1(ni)(x1)iR_{n,m}(x) = \sum_{i=0}^{m-1} \binom{n}{i} t^i = \sum_{i=0}^{m-1} \binom{n}{i} (x-1)^i

Extracting Coefficient of xdx^d

Expanding (x1)i=j=0i(ij)xj(1)ij(x-1)^i = \sum_{j=0}^{i} \binom{i}{j} x^j (-1)^{i-j}, the coefficient of xdx^d in Rn,m(x)R_{n,m}(x) is:

[xd]Rn,m(x)=i=dm1(ni)(id)(1)id[x^d] R_{n,m}(x) = \sum_{i=d}^{m-1} \binom{n}{i} \binom{i}{d} (-1)^{i-d}

Key Identity

Using the identity (ni)(id)=(nd)(ndid)\binom{n}{i}\binom{i}{d} = \binom{n}{d}\binom{n-d}{i-d}, we get:

[xd]Rn,m(x)=(nd)k=0md1(ndk)(1)k[x^d] R_{n,m}(x) = \binom{n}{d} \sum_{k=0}^{m-d-1} \binom{n-d}{k} (-1)^k

where k=idk = i - d.

Formula for C(n, m, d)

C(n,m,d)=(nd)k=0md1(ndk)(1)kC(n, m, d) = \left| \binom{n}{d} \sum_{k=0}^{m-d-1} \binom{n-d}{k} (-1)^k \right|

Modular Arithmetic

Since 999999937999999937 is prime, we can use:

  • Fermat’s little theorem for modular inverses.
  • Lucas’ theorem if needed (but here n<pn < p, so standard modular arithmetic suffices since p=999999937>1013p = 999999937 > 10^{13}… actually p109<1013p \approx 10^9 < 10^{13}).

Wait: p=999999937<n=1013p = 999999937 < n = 10^{13}. So we need Lucas’ theorem or Granville’s generalization.

Actually, since pp is prime: (nd)modp\binom{n}{d} \bmod p can be computed using Lucas’ theorem: write nn and dd in base pp.

For n=1013n = 10^{13} in base p=999999937p = 999999937: n=10000p+rn = 10000 \cdot p + r where r=101310000999999937=10139999999370000=630000r = 10^{13} - 10000 \cdot 999999937 = 10^{13} - 9999999370000 = 630000.

So (nd)(10000d1)(630000d0)(modp)\binom{n}{d} \equiv \binom{10000}{d_1} \binom{630000}{d_0} \pmod{p} where d=d1p+d0d = d_1 \cdot p + d_0.

Since d=104<pd = 10^4 < p, we have d1=0,d0=104d_1 = 0, d_0 = 10^4. So (nd)(100000)(63000010000)=(63000010000)(modp)\binom{n}{d} \equiv \binom{10000}{0} \cdot \binom{630000}{10000} = \binom{630000}{10000} \pmod{p}.

Wait, Lucas says (nd)(nidi)(modp)\binom{n}{d} \equiv \prod \binom{n_i}{d_i} \pmod{p} where n=nipin = \sum n_i p^i and d=dipid = \sum d_i p^i.

n=1013n = 10^{13}, p=999999937p = 999999937. n=10p+(101310999999937)n = 10 \cdot p + (10^{13} - 10 \cdot 999999937). Let me compute: 10999999937=999999937010 \cdot 999999937 = 9999999370. 1013=1000000000000010^{13} = 10000000000000. 10139999999370=...10^{13} - 9999999370 = .... Actually 1013/p10000.0000...10^{13} / p \approx 10000.0000.... 10000p=999999937000010000 \cdot p = 9999999370000. 10139999999370000=63000010^{13} - 9999999370000 = 630000.

So n=10000p+630000n = 10000 \cdot p + 630000. And d=10000<pd = 10000 < p, so d=0p+10000d = 0 \cdot p + 10000.

By Lucas: (nd)(100000)(63000010000)=(63000010000)(modp)\binom{n}{d} \equiv \binom{10000}{0} \cdot \binom{630000}{10000} = \binom{630000}{10000} \pmod{p}.

Similarly for (ndk)\binom{n-d}{k}: nd=1013104n - d = 10^{13} - 10^4. In base pp: 10000p+63000010000=10000p+62000010000 \cdot p + 630000 - 10000 = 10000 \cdot p + 620000.

So (ndk)(10000k1)(620000k0)(modp)\binom{n-d}{k} \equiv \binom{10000}{k_1} \binom{620000}{k_0} \pmod{p} where k=k1p+k0k = k_1 p + k_0.

Since md1=10121041<1012m - d - 1 = 10^{12} - 10^4 - 1 < 10^{12}, kk ranges up to about 101210^{12}, which means k1k_1 can be 0 or 1 (since p109p \approx 10^9). We need k110000k_1 \leq 10000.

The alternating sum k=0M(Nk)(1)k\sum_{k=0}^{M} \binom{N}{k}(-1)^k where M=md1M = m-d-1 and N=ndN = n-d can be computed by splitting on digits of kk in base pp.

Editorial

Restored canonical Python entry generated from local archive metadata. We compute the alternating sum k=0md1(ndk)(1)kmodp\sum_{k=0}^{m-d-1} \binom{n-d}{k}(-1)^k \bmod p using digit decomposition with Lucas’ theorem. Finally, multiply and take absolute value mod pp.

Pseudocode

Compute $\binom{n}{d} \bmod p$ using Lucas' theorem
Compute the alternating sum $\sum_{k=0}^{m-d-1} \binom{n-d}{k}(-1)^k \bmod p$ using digit decomposition with Lucas' theorem
Multiply and take absolute value mod $p$

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Complexity Analysis

  • Time: O(p)O(p) for precomputing factorials, plus manageable sum computation.
  • Space: O(p)O(p) for factorial arrays.

Answer

472294837\boxed{472294837}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_498/solution.cpp
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll MOD = 999999937;

vector<ll> fact, inv_fact;

ll power(ll base, ll exp, ll mod) {
    ll result = 1;
    base %= mod;
    if (base < 0) base += mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

void precompute(int n) {
    fact.resize(n + 1);
    inv_fact.resize(n + 1);
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
        fact[i] = fact[i - 1] * i % MOD;
    inv_fact[n] = power(fact[n], MOD - 2, MOD);
    for (int i = n - 1; i >= 0; i--)
        inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD;
}

ll C_small(ll n, ll k) {
    if (k < 0 || k > n || n < 0) return 0;
    if (n >= MOD) return 0; // should not happen for small calls
    return fact[n] % MOD * inv_fact[k] % MOD * inv_fact[n - k] % MOD;
}

ll C_lucas(ll n, ll k) {
    if (k < 0 || k > n) return 0;
    ll result = 1;
    while (n > 0 || k > 0) {
        ll ni = n % MOD;
        ll ki = k % MOD;
        if (ki > ni) return 0;
        result = result * C_small(ni, ki) % MOD;
        n /= MOD;
        k /= MOD;
    }
    return result;
}

int main() {
    ll n = 10000000000000LL;  // 10^13
    ll m = 1000000000000LL;   // 10^12
    ll d = 10000LL;           // 10^4

    precompute(MOD - 1);

    // C(n, m, d) = |C(n, d) * sum_{k=0}^{m-d-1} C(n-d, k) * (-1)^k| mod p
    ll binom_nd = C_lucas(n, d);

    // Using identity: sum_{k=0}^{M} (-1)^k C(N,k) = (-1)^M * C(N-1, M)
    ll N = n - d;      // 10^13 - 10^4
    ll M = m - d - 1;  // 10^12 - 10^4 - 1

    // C(N-1, M) mod p using Lucas
    ll binom_alt = C_lucas(N - 1, M);

    // C(n,m,d) = |C(n,d) * (-1)^M * C(N-1,M)|
    // Since we take absolute value, the sign doesn't matter:
    // C(n,m,d) = C(n,d) * C(N-1,M)
    ll answer = binom_nd * binom_alt % MOD;

    cout << answer << endl;

    return 0;
}