Home > Industries > Energy

Source: Internet
Author: User

According to Lucas ' theorem, we have

Ans (n,k) =ans (⌊np⌋,⌊kp⌋−1) ∗∑i=0p−1 (N%PI) + (⌊n/p⌋⌊k/p⌋) ∑i=0k%p (n%pi) ans (n,k) =ans (\lfloor\frac n p \rfloor,\lfloor \ Frac k p\rfloor-1) *\sum_{i=0}^{p-1}\binom{n\%p}i+\binom{\lfloor N/p\rfloor}{\lfloor k/p\rfloor}\sum_{i=0}^{k\%p}\ Binom{n\%p}i

One of the ans-ans-term recursive computations, combined number with Lucas violence calculation, and type O (p2) o (p^2) pretreatment.

Complexity O (P2+TLOGNLOGP) o (P^2+t\log n\log p).

#include <cstdio> #include <algorithm> using namespace std; #define LL Long Long const int p=2333; int pow (int base,int k) {int ret=1; for (; k;k>>=1,base=base*base%p) if (k&1) ret=ret*base%p; return ret; } int fac[p+10],inv[p+10],sum[p+10][p+10]; int C (LL n,ll k) {if (k>n) return 0; if (n<p&&k<p) return fac[n]*inv[k]%p*inv[n-k]%p; return C (n%p,k%p) *c (n/p,k/p)%p; } int Solve (LL n,ll k) {int ret=0,tem=0; if (n<p&&k<p) return sum[n][k]; Return (Sum[n%p][p-1]*solve (n/p,k/p-1)%p+sum[n%p][k%p]*c (n/p,k/p)%p)%p; } int main () {LL n,k; Inv[0]=fac[0]=1; for (int i=1;i<p;i++) fac[i]=fac[i-1]*i%p; Inv[p-1]=pow (fac[p-1],p-2); for (int i=p-2;i;i--) inv[i]=inv[i+1]* (i+1)%p; for (int i=0;i<p;i++) {sum[i][0]=1; for (int j=1;j<p;j++) sum[i][j]= (Sum[i][j-1]+c (i,j))%p; } int T; scanf ("%d", &t); while (t--) {scanf ("%lld%lld",&N,&K); printf ("%d\n", Solve (n,k)); } }