codeforces 893E(组合数学&组合数取mod)

来源:互联网 发布:织梦wap模板 编辑:程序博客网 时间:2024/06/10 12:38

题目链接:http://codeforces.com/problemset/problem/893/

先质因子分解此题可以转化为n个数字放入y个空格中所以用插空法C(y-1+t,y-1)计算出情况

在这里数字还可以是负数所以在原来的基础上将偶数个数字变成负数C(y,0)+C(y,2)+C(y,4)+...

化简为2^(y-1)

推倒:

(1+x)^n=C(n,0)+C(n,1)*x+...+C(n,n)*x^n

取x=-1得到C(n,0)-C(n,1)+...+(-1)^n*C(n,n)=2^n

取x=1得到C(n,0)+C(n,1)+...+(-1)^n*C(n,n)=0

推出 C(n,0)+C(n,2)+...=2^n-1

计算2^n-1的时候用快速幂即可

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <vector>using namespace std;typedef long long int LL;const int maxn=2e6;const int mod=(1e9)+7;LL fac[maxn]={};LL num_pow(LL a,LL k){    LL res=1,tem=a;    for(;k;k>>=1){        if(k&1) res=(res*tem)%mod;        tem=(tem*tem)%mod;    }    return res;}LL C(LL n,LL m){    return (fac[n]*num_pow(fac[m],mod-2))%mod*num_pow(fac[n-m],mod-2)%mod; //阶层取mod}LL Lucas(LL n,LL m )  {         //不要用Lucas会T的(在这里写出来是为了以后方便用)    if(m==0) return 1;    else return  (C(n%mod,m%mod)*Lucas(n/mod,m/mod))%mod;}int main(){    LL q,n,k;    scanf("%lld",&q);    fac[0]=1;    for(int i=1;i<2e6;i++) fac[i]=fac[i-1]*i%mod;    while(q--){        scanf("%lld%lld",&n,&k);        LL ans=num_pow(2,k-1);        for(LL i=2;i*i<=n;i++)if(n%i==0){            int t=0;            while(n%i==0){                t++;                n/=i;            }            ans=ans*C(t+k-1,k-1)%mod;        }        if(n>1) ans=(ans*k)%mod;        printf("%lld\n",ans);    }    return 0;}


原创粉丝点击