版权声明:蒟蒻Blog随意转载 https://blog.csdn.net/a1799342217/article/details/81570065
lucas
来抄推柿子:
预处理出 的 ,组合用lucas,递归做就好了。
代码:
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 5000
#define F inline
using namespace std;
typedef long long LL;
const LL p=2333;
LL t,n,k,f[N][N],c[N],iv[N];
F char readc(){
static char buf[100000],*l=buf,*r=buf;
if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
return l==r?EOF:*l++;
}
F LL _read(){
LL x=0; char ch=readc();
while (!isdigit(ch)) ch=readc();
while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
return x;
}
F void writec(LL x){ if (x>9) writec(x/10); putchar(x%10+48); }
F void _write(LL x){ writec(x),puts(""); }
LL lucas(LL n,LL m){
if (n<m) return 0;
if (n<p&&m<p) return c[n]*iv[m]%p*iv[n-m]%p;
return lucas(n/p,m/p)*lucas(n%p,m%p)%p;
}
F void Make(){
c[0]=c[1]=iv[0]=iv[1]=1;
for (int i=2;i<=p;i++)
c[i]=c[i-1]*i%p,iv[i]=p-p/i*iv[p%i]%p;
for (int i=2;i<=p;i++)
iv[i]=iv[i-1]*iv[i]%p;
for (int i=0;i<=p;i++) f[i][0]=f[0][i]=1;
for (int i=1;i<=p;i++)
for (int j=1;j<=p;j++)
f[i][j]=(lucas(i,j)+f[i][j-1])%p;
}
LL C(LL n,LL k){
if (n<p&&k<p) return f[n][k];
return (C(n/p,k/p-1)*f[n%p][p-1]%p+lucas(n/p,k/p)*f[n%p][k%p]%p)%p;
}
int main(){
for (t=_read(),Make();t;t--)
n=_read(),k=_read(),_write(C(n,k));
return 0;
}