Lucas定理
ans=C(n,0)+C(n,1)+……+C(n,k)
=C(n/2333,0)*C(n%2333,0)+C(n/2333,0)*C(n%2333,1)+……+C(n/2333,0)*C(n%2333,2332)+C(/2333,1)*C(n%2333,0)+……+C(n/2333,k/2333)*(n%2333,k%2333)
=∑C(n/2333,j)*sum[n%2333][2332](0<=j<k/2333)+C(n/2333,k/2333)*sum[n%2333][k%2333]
cal(n,k)=cal(n/2333,k/2333-1)*sum[n%2333][2332]+Lucas(n/2333,k/2333)*sum[n%2333][k%2333]
注意首碼和的處理
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<iostream>#define mod 2333using namespace std;int T;long long c[mod][mod],sum[mod][mod];long long Lucas(long long x,long long y){if (x<y || y<0) return 0; if (x<mod && y<mod) return c[x][y];return Lucas(x/mod,y/mod)*c[x%mod][y%mod]%mod;}long long cal(long long n,long long k){if (k<0) return 0;return (cal(n/mod,k/mod-1)*sum[n%mod][mod-1]+Lucas(n/mod,k/mod)*sum[n%mod][k%mod])%mod;}int main(){c[0][0]=1;sum[0][0]=1;for (int i=1;i<mod;i++) sum[0][i]=1;for (int i=1;i<mod;i++){c[i][0]=sum[i][0]=1;for (int j=1;j<=i;j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;for (int j=1;j<mod;j++) sum[i][j]=(sum[i][j-1]+c[i][j])%mod;}scanf("%d",&T);while (T--){long long n,k;scanf("%lld%lld",&n,&k);printf("%lld\n",cal(n,k));}return 0;}