CodeForces 615 D. Multipliers(数论)

来源:互联网 发布:无法启动承载网络 编辑:程序博客网 时间:2024/05/21 17:29

Description
给出m个素数,这m个素数相乘得到一个数n,求n所有因子的乘积
Input
第一行为一整数m素数数量,之后m个素数pi(1<=m<=200000,2<=pi<=200000)
Output
输出n所有因子的乘积,结果模1e9+7
Sample Input
3
2 3 2
Sample Output
1728
Solution
首先记录每种素因子i的数量num[i],设有res个不同的素因子,令
这里写图片描述
这里写图片描述
那么由费马小定理有
这里写图片描述
Code

#include<cstdio>#include<iostream>#include<cstring>using namespace std;#define maxn 22222#define mod 1000000007lltypedef long long ll;int n,num[10*maxn],p[maxn],res;ll l[maxn],r[maxn];ll mod_pow(ll a,ll b,ll p){    ll ans=1ll;    a%=p;    while(b)    {        if(b&1)ans=(ans*a)%p;        a=(a*a)%p;        b>>=1;    }    return ans;}int main(){    while(~scanf("%d",&n))    {        res=0;        memset(num,0,sizeof(num));        while(n--)        {            int temp;            scanf("%d",&temp);            if(!num[temp])p[++res]=temp;            num[temp]++;        }        l[0]=r[res+1]=1ll;        for(int i=1;i<=res;i++)l[i]=l[i-1]*(num[p[i]]+1)%(mod-1);        for(int i=res;i>=1;i--)r[i]=r[i+1]*(num[p[i]]+1)%(mod-1);        ll ans=1ll;        for(int i=1;i<=res;i++)        {            ll a=1ll*num[p[i]]*(num[p[i]]+1)/2%(mod-1)*l[i-1]%(mod-1)*r[i+1]%(mod-1);            ans=ans*mod_pow(p[i],a,mod)%mod;        }        printf("%I64d\n",ans);    }    return 0;}
1 0