【bzoj4591】[Shoi2015]超能粒子炮·改 Lucas定理_數論

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;}`

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD