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

题目链接: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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值