[BZOJ4591][SHOI2015]超能粒子炮·改(Lucas+组合数学)

题目:

我是超链接

题解:

也就是求 ki=0Cin(%mod) ∑ i = 0 k C n i ( % m o d )
n,k这么大,Lucas没跑了,我们先画柿子(模意义下
Smn S n m 意为 mi=0Cin ∑ i = 0 m C n i
Lucas定理:

Skn=i=0kCin=i=0k(Ci%pn%pCi/pn/p) S n k = ∑ i = 0 k C n i = ∑ i = 0 k ( C n % p i % p ∗ C n / p i / p )

i=ip
=i=0k/p1(Cin/pj=0p1Cjn%p)+Ck/pn/pi=0k%pCin%p = ∑ i = 0 k / p − 1 ( C n / p i ∗ ∑ j = 0 p − 1 C n % p j ) + C n / p k / p ∗ ∑ i = 0 k % p C n % p i

=i=0k/p1Cin/pSp1n%p+Ck/pn/pSk%pn%p = ∑ i = 0 k / p − 1 C n / p i ∗ S n % p p − 1 + C n / p k / p ∗ S n % p k % p
=Sk/p1n/pSp1n%p+Ck/pn/pSk%pn%p = S n / p k / p − 1 ∗ S n % p p − 1 + C n / p k / p ∗ S n % p k % p

我们预处理就好了
注意两个问题,一个是s[0][i]=1,另一个是要时时刻刻注意s的意义,当m>n的时候s并不是0= =

代码:

#include 
#define LL long long 
using namespace std;
const int p=2333;
int c[p+5][p+5],s[p+5][p+5];
LL Lucas(LL n,LL m)
{
    if (!m) return 1;
    if (n<m) return 0; LL ans=1;
    for (;m;n/=p,m/=p) ans=ans*c[n%p][m%p]%p;
    return ans;
}
LL askS(LL n,LL k)
{
    if (n,k/p-1)%p+Lucas(n/p,k/p)*s[n%p][k%p]%p)%p;
    return ans;
}
void init()
{
    c[0][0]=1;
    for (int i=0;is[0][i]=1;
    for (int i=1;i0]=1,s[i][0]=1;
        for (int j=1;j1][j]+c[i-1][j-1])%p;
            s[i][j]=(s[i][j-1]+c[i][j])%p;
        }
    }
}
int main()
{
    init();
    int T;scanf("%d",&T);
    while (T--)
    {
        LL n,k;
        scanf("%lld%lld",&n,&k);
        printf("%lld\n",askS(n,k));
    }
}

你可能感兴趣的:(组合数学,Lucas,省选)