Codeforces Round #338 (Div. 2)D. Multipliers【費馬小定理+組合數學】
Ayrat has number n, represented as it's prime factorization pi of size m, i.e. n = p1·p2·...·pm. Ayrat got secret information that that the product of all divisors of n taken modulo 109 + 7 is the password to the secret data base. Now he wants to calculate this value.
Input
The first line of the input contains a single integer m
The second line contains m primes numbers pi (2 ≤ pi ≤ 200 000).
OutputPrint one integer — the product of all divisors of n modulo 109 + 7.
Examples input2 2 3output
36input
3 2 3 2output
1728Note
In the first sample n
In the second sample 2·3·2 = 12. The divisors of 12 are 1, 2, 3, 4, 6 and 12. 1·2·3·4·6·12 = 1728.
一個數的素因子為p1,p2,p3.....其數量為t[1],t[2]....t[n],共 kind種素數那麼這個數的因子個數為 sum=(t[1]+1)*(t[2]+1)*****(t[n]+1); 求出每一個素因子對答案的貢獻,對一個素因子P[i]則數N中包含素因子p[i]的數有p[i]出現一次與的因數的個數為剩下的因子的素因子的個數,出現兩次的因數的個數同為其其它的因數的個數。最後這些因數將最終乘在一起所以可以計算出素因子p[i]總共被乘的(1+2+...+t[i])*(不包含素因子p[i]的因數的個數==sum/(t[i]+1))所以可得素因子p[i]的貢獻為p[i]^((t[i]*(t[i]+1)/2)*(sum/(t[i]+1)));
費馬小定理 :假如p是素數,且a與p互質,那麼a^(p-1) = 1 (mod p)。 由此可以推得 a^x%p==> a^(x%(p-1))%p考慮x會非常的大且還有除2操作,所以對其取餘之後再除2則結果可能不正確,因此可以對(2*MOD-2)取餘即可。/* ***********************************************
Author : ryc
Created Time : 2016-08-12 Friday
File Name : E:\acm\codeforces\367D.cpp
Language : c++
Copyright 2016 ryc All Rights Reserved
************************************************ */
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<string>
#define MOD 1000000007ll
using namespace std;
const int maxn=200010;
typedef long long LL;
LL t[maxn];
LL Pow(LL a,LL b){
if(b==0)return 1%MOD;
long long temp=Pow(a,b>>1);
temp=temp*temp%MOD;
if(b&1)temp=temp*a%MOD;
return temp;
}
int main()
{
LL n,m;cin>>m;
for(LL i=1;i<=m;++i){
scanf("%lld",&n);t[n]++;
}
LL sum=1;
for(int i=1;i<maxn;++i){
if(t[i])sum=sum*(t[i]+1)%(2*MOD-2);
}
LL ans=1;
for(LL i=1;i<maxn;++i){
if(t[i]){
ans=ans*Pow(i,sum*t[i]/2ll%(MOD-1))%MOD;
}
}
printf("%lld\n",ans);
return 0;
}