Home > Industries > Energy

Source: Internet
Author: User

Lucas theorem

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)

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]

Attention to prefix and processing

#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> # include<algorithm> #include <iostream> #define MOD 2333 using 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; }