Multipliers codeforces -费马小定理

来源:互联网 发布:软件培训机构排行榜 编辑:程序博客网 时间:2024/05/19 23:29

http://codeforces.com/contest/615/problem/D


Ayrat has number n, represented as it's prime factorizationpi of sizem, i.e. n = p1·p2·...·pm. Ayrat got secret information that that the product of all divisors of n taken modulo109 + 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 (1 ≤ m ≤ 200 000) — the number of primes in factorization ofn.

The second line contains m primes numberspi (2 ≤ pi ≤ 200 000).

Output

Print one integer — the product of all divisors of n modulo109 + 7.

Sample test(s)
Input
22 3
Output
36
Input
32 3 2
Output
1728
Note

In the first sample n = 2·3 = 6. The divisors of6 are 1, 2, 3 and 6, their product is equal to1·2·3·6 = 36.

In the second sample 2·3·2 = 12. The divisors of12 are 1, 2, 3, 4, 6 and 12. 1·2·3·4·6·12 = 1728.


题意:给n各质数,求这n各质数相乘的值m的所有的因子的乘积mod(1e9+7)。例如给出3个质数2,2,3。乘积为12。让求1×2×3×4×6×12mod(1e9+7)的值。

解题思路:

小于200000的素数不是很多,所以肯定有很多重复的数字。开一个map,记录素数和对应的个数。

然后看组合情况,假设素数j有k个,那么j有(kj+1)种出现的形式,所以对于所有的素数就有(k1+1)×(k2+1)×(k3+1)×(k4+1)*...*(kn+1)= sum种组合方式。不考虑j时就有sum/(kj+1)种。假设其中一种除j外的乘积为a,那么就有(a)×(a×j)×(a× j^2)×(a×j^3)×...×(a×j^kj) = (a^(kj+1))×(j^(kj*(kj+1)/2)) .所以在所有的情况中共乘了j^((kj*(kj+1)/2))^(sum/(kj+1)).对于每一个素数求解一遍,乘积mod(1e9+7)为结果。

但是因为数据量过大,会导致sum爆 long long,所以在求解每一个j的sum/(kj+1)时要想办法。

我们用cnt(j)表示j^((kj*(kj+1)/2))

所以我们用d表示遍历到j时的种类的个数,即d(j)= (k1+1)×(k2+1)×(k3+1)×...×(kj+1)

然后

d(0)=1;ans = 1;for j in ma:    cnt(j)    ans=ans^(k+1)*cnt(j)^d(j-1);    d(j)ans为最终结果例如我们有一组数据2 2 3 5 5
cnt(1) = 2^(2*3/2)=8,cnt(2)=3^(1+2/2)cnt(3)j = 1:    ans = cnt(1)^1    d(1)=d(0)*(k1+1)=(k1+1)j = 2:    ans = cnt(1)^1^(k2+1)*cnt(2)^(k1+1)    d(2) =d(1)*(k2+1)= (k1+1)*(k2+1)j = 3:    ans = cnt(1)^1^(k2+1)^(k3+1)*cnt(2)^(k1+1)(k3+1)*cnt(3)^(k1+1)*(k2+1)    d(3) = d(2)*(k3+1)

并且在本解法中没有sum/k的运算。由费马小定理若p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p),则a^x≡a^(x%(m-1))(mod p)
所以再加上%mod得到:
d(0)=1;ans = 1;for j in ma:    cnt(j)    ans=ans^(k+1)%mod*cnt(j)^d(j-1)%mod;    d(j)%(mod-1)然后再用快速幂解法
ll fast_mod(ll n,ll m,ll mod){ll ans =1;while(m){if(m&1)ans = ans*n%mod;m>>=1;n = n*n%mod;}return ans;}

即可快速的求解出ans。
#include<bits/stdc++.h>using namespace std;typedef long long ll;ll p[200050];map<int ,ll>MA;const ll mod = 1e9+7;ll fast_mod(ll n,ll m,ll mod){ll ans =1;while(m){if(m&1)ans = ans*n%mod;m>>=1;n = n*n%mod;}return ans;}int main(){int T;cin>>T;MA.clear();for(int i = 0;i<T;i++){int a;scanf("%d",&a);MA[a]++;}map<int,ll>::iterator it;ll sum = 1;ll ans = 1;for(it = MA.begin();it!=MA.end();it++){int cnt = fast_mod(it->first,(it->second+1)*it->second/2,mod);ans=fast_mod(ans,it->second+1,mod)*fast_mod(cnt,sum,mod)%mod;sum=sum*(it->second+1)%(mod-1);}printf("%I64d\n",ans);}
1 0