洛谷P4345 [SHOI2015] 超能粒子炮·改 题解 Lucas定理

题目链接:https://www.luogu.com.cn/problem/P4345

解题思路:

5577264ba3d46e1afd68603211c24d8d

示例程序:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll p = 2333;

ll c[p+5][p+5], sum[p+5][p+5];

void init() {
    for (int i = 0; i < p; i++) {
        for (int j = 0; j <= i; j++) {
            if (j == 0 || j == i)
                c[i][j] = 1;
            else
                c[i][j] = (c[i-1][j-1] + c[i-1][j]) % p;
        }
        for (int j = 0; j < p; j++) {
            sum[i][j] = c[i][j];
            if (j)
                (sum[i][j] += sum[i][j-1]) %= p;
        }
    }
}

ll C(ll n, ll k) {
    if (k == 0 || k == n)
        return 1;
    if (k > n)
        return 0;
    if (n < p)
        return c[n][k];
    return C(n/p, k/p) * C(n%p, k%p) % p;
}

ll f(ll n, ll k) {
    if (n < p && k < p)
        return sum[n][k];
    if (p == 0)
        return 1;
    return ( f(n/p, k/p-1) * f(n%p, p-1) + C(n/p, k/p) * f(n%p, k%p) ) % p;
}

int T;
ll n, k;

int main() {
    init();
    scanf("%d", &T);
    while (T--) {
        scanf("%lld%lld", &n, &k);
        printf("%lld\n", f(n, k));
    }
    return 0;
}
posted @ 2026-05-21 16:53  quanjun  阅读(7)  评论(0)    收藏  举报