codeforces893E Counting Arrays 排列组合

题目链接:戳这里

题目大意:给出x和y,求一个长度为y的序列,其乘积为x,允许有负数,求这种序列的个数,对1e9+7取模。

题解:经典的排列组合问题。

先对x进行质因数分解,那么答案就是C(t+y-1,t)之和,t为每个质因数的指数。

再考虑负数,无非就是只会出现偶数个负数,答案再乘上2^(y-1)即可。

代码:

#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef long long LL;
int read()
{
    char c;int sum=0,f=1;c=getchar();
    while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0' && c<='9'){sum=sum*10+c-'0';c=getchar();}
    return sum*f;
}
int T,x,y;
LL fac[2000005];
LL ksm(LL a,LL b)
{
    LL ret=1;
    while(b)
    {
        if(b&1)ret=ret*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ret;
}
void init()
{
    fac[0]=1;
    for(int i=1;i<2000005;i++)
    fac[i]=(fac[i-1]*i)%mod;
}
LL C(LL n,LL m)
{
    return (fac[n]*ksm(fac[m],mod-2))%mod*ksm(fac[n-m],mod-2)%mod;
}
int main()
{
    init();
    T=read();
    while(T--)
    {
        x=read();y=read();
        int c=0;
        LL ans=ksm(2,y-1);
        for(int i=2;i*i<=x;i++)
        {
            if(x%i==0)
            {
                int t=0;
                while(x%i==0)
                {
                    t++;
                    x/=i;
                }
                ans=(ans*C(t+y-1,t))%mod;
            }
        }
        if(x>1)
        ans=(ans*y)%mod;
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39791208/article/details/79384834