BZOJ4591 [Shoi2015]超能粒子炮·改(洛谷P4345)

版权声明:蒟蒻Blog随意转载 https://blog.csdn.net/a1799342217/article/details/81570065

lucas

BZOJ题目传送门
洛谷题目传送门

推柿子:

(5) f ( n , k ) = i = 0 k ( n i ) % p (6) = i = 0 k ( n / p i / p ) ( n % p i % p ) (7) = i = 0 k / p 1 ( n / p i ) j = 0 p 1 ( n % p j ) + ( n / p k / p ) i = 0 k % p ( n % p i ) (8) = f ( n / p , k / p 1 ) f ( n % p , p 1 ) + ( n / p k / p ) f ( n % p , k % p )

预处理出 n < p , k < p f ( n , k ) ,组合用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;
}

猜你喜欢

转载自blog.csdn.net/a1799342217/article/details/81570065
今日推荐