---
id: 1890
lang: en
translation_of: index.md
source_sha256: 45a24bd8b557a70724eb66a4d3ca45427daa57303c7c57bbadc49ab349f7a089
title: "UOJ#275: [Tsinghua Training Camp 2016] Binomial Coefficient Problem — Solution"
description: "Split the binomial coefficient with Lucas, i.e. write both numbers in base k and multiply digit by digit; count all cases, then subtract with a digit DP."
tags: ["OI","Math","Number Theory","Binomial Coefficients","Lucas's Theorem"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem name 组合数问题 rendered as “Binomial Coefficient Problem”, as in the NOIp 2016 edition."
  - "连乘序列 (literally “continued-product sequence”) rendered as “the whole product”."
---

## Main Idea

So <span class="slang" data-zh="您妈" data-gloss="nín mā, the polite 您 “you” plus 妈 “mom”: a mock-polite form of the curse 你妈 “your mom”. Here an intensifier like “goddamn”.">goddamn</span> <span class="slang" data-zh="神仙" data-gloss="shénxiān, “immortal”. Someone, or a problem or idea, so brilliant it seems superhuman.">godlike</span>.

Consider using Lucas to decompose the binomial coefficient:

$$
{n \choose m} = {n / k \choose m / k} {n \mod k \choose m \mod k}
$$

<!--more-->

This is actually equivalent to decomposing the two numbers in base $k$, then multiplying digit by digit to compute the binomial coefficient. We consider turning the two numbers into two base-$k$ sequences, ordered from the ones place toward the tens place:

$$
\begin{gathered} \text{Set } p_n = \{ n_1, n_2, \dots \}, p_m = \{ m_1, m_2, \cdots \} \\ ans = \prod_{i = 1} {n_i \choose m_i} \end{gathered}
$$

Then we find that we only need to count, in base $k$, the pairs $(i, j), i \leq n, j \leq \min(i, m)$ satisfying $\exists k, i_k < j_k$; such a term ${i_k \choose j_k} = 0$ then makes the whole product $0$, which achieves divisibility by $k$. Count all the cases, then rig up a digit DP to subtract these, and that’s it.

## Code

```cpp
// UOJ275.cpp
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int MAX_N = 110, mod = 1e9 + 7, inv2 = 500000004;

int T, k, dp[MAX_N][2][2], digit_n[MAX_N], digit_m[MAX_N];
ll n, m;

int main()
{
    scanf("%d%d", &T, &k);
    while (T--)
    {
        scanf("%lld%lld", &n, &m);
        memset(dp, 0, sizeof(dp));
        ll a = n, b = m;
        memset(digit_n, 0, sizeof(digit_n));
        memset(digit_m, 0, sizeof(digit_m));
        while (a)
            digit_n[++digit_n[0]] = a % k, a /= k;
        while (b)
            digit_m[++digit_m[0]] = b % k, b /= k;
        for (int i = 0; i < k; i++)
            for (int j = 0; j <= i; j++)
                dp[1][i > digit_n[1]][j > digit_m[1]]++;
        for (int i = 2; i <= max(digit_n[0], digit_m[0]); i++)
            for (int da = 0; da <= k - 1; da++)
                for (int db = 0; db <= da; db++)
                {
                    // basic preperation;
                    if (da < digit_n[i] && db < digit_m[i])
                    {
                        dp[i][0][0] = (1LL * dp[i][0][0] + dp[i - 1][0][0] + dp[i - 1][0][1]) % mod;
                        dp[i][0][0] = (1LL * dp[i][0][0] + dp[i - 1][1][0] + dp[i - 1][1][1]) % mod;
                    }
                    else if (da < digit_n[i] && db > digit_m[i])
                    {
                        dp[i][0][1] = (1LL * dp[i][0][1] + dp[i - 1][0][0] + dp[i - 1][0][1]) % mod;
                        dp[i][0][1] = (1LL * dp[i][0][1] + dp[i - 1][1][0] + dp[i - 1][1][1]) % mod;
                    }
                    else if (da > digit_n[i] && db < digit_m[i])
                    {
                        dp[i][1][0] = (1LL * dp[i][1][0] + dp[i - 1][0][0] + dp[i - 1][0][1]) % mod;
                        dp[i][1][0] = (1LL * dp[i][1][0] + dp[i - 1][1][0] + dp[i - 1][1][1]) % mod;
                    }
                    else if (da > digit_n[i] && db > digit_m[i])
                    {
                        dp[i][1][1] = (1LL * dp[i][1][1] + dp[i - 1][0][0] + dp[i - 1][0][1]) % mod;
                        dp[i][1][1] = (1LL * dp[i][1][1] + dp[i - 1][1][0] + dp[i - 1][1][1]) % mod;
                    }
                    else if (da == digit_n[i] && db == digit_m[i])
                    {
                        dp[i][0][0] = (1LL * dp[i][0][0] + dp[i - 1][0][0]) % mod;
                        dp[i][1][0] = (1LL * dp[i][1][0] + dp[i - 1][1][0]) % mod;
                        dp[i][0][1] = (1LL * dp[i][0][1] + dp[i - 1][0][1]) % mod;
                        dp[i][1][1] = (1LL * dp[i][1][1] + dp[i - 1][1][1]) % mod;
                    }
                    else if (da < digit_n[i] && db == digit_m[i])
                    {
                        dp[i][0][0] = (1LL * dp[i][0][0] + dp[i - 1][0][0] + dp[i - 1][1][0]) % mod;
                        dp[i][0][1] = (1LL * dp[i][0][1] + dp[i - 1][0][1] + dp[i - 1][1][1]) % mod;
                    }
                    else if (da == digit_n[i] && db < digit_m[i])
                    {
                        dp[i][0][0] = (1LL * dp[i][0][0] + dp[i - 1][0][0] + dp[i - 1][0][1]) % mod;
                        dp[i][1][0] = (1LL * dp[i][1][0] + dp[i - 1][1][0] + dp[i - 1][1][1]) % mod;
                    }
                    else if (da > digit_n[i] && db == digit_m[i])
                    {
                        dp[i][1][0] = (1LL * dp[i][1][0] + dp[i - 1][0][0] + dp[i - 1][1][0]) % mod;
                        dp[i][1][1] = (1LL * dp[i][1][1] + dp[i - 1][0][1] + dp[i - 1][1][1]) % mod;
                    }
                    else
                    {
                        dp[i][0][1] = (1LL * dp[i][0][1] + dp[i - 1][0][0] + dp[i - 1][0][1]) % mod;
                        dp[i][1][1] = (1LL * dp[i][1][1] + dp[i - 1][1][0] + dp[i - 1][1][1]) % mod;
                    }
                }
        int ans = 1LL * (n + 1LL) % mod * ((n + 2LL) % mod) % mod * inv2 % mod;
        if (n > m)
            ans = (1LL * ans - 1LL * ((n - m + 0LL) % mod) * ((n - m + 1LL) % mod) % mod * inv2 % mod + mod) % mod;
        ans = (1LL * ans - dp[max(digit_n[0], digit_m[0])][0][0] + mod) % mod;
        printf("%d\n", ans);
    }
    return 0;
}
```
