Description
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 (1 ≤ m ≤ 200 000) — the number of primes in factorization of n.
The second line contains m primes numbers pi (2 ≤ pi ≤ 200 000).
Output
Print one integer — the product of all divisors of n modulo 109 + 7.
Sample Input
2 2 3
36
3 2 3 2
1728
Hint
In the first sample n = 2·3 = 6. The divisors of 6 are 1, 2, 3 and 6, their product is equal to 1·2·3·6 = 36.
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.
这里要求算出m个数相乘结果的因子相乘%10^9+7的结果,因为这些数全部都是素数,所以就不需
分解,如果不是素数只需要分解成素数就可以了。
解法:
可以直接统计每个素数出现的次方是多少就行了
算法:
当前素数可以取1-ai(ai表示此种素数的个数)
那么其前面所有的数取法有(a1+1)*(a2+1)...*(a[i-1]+1)*(a[i+1]+1)...*(an+1)
这样就可以直接算出次方是多少了,但是可能longlong都存不下,
那么就要用一个定理费马小定理:a^(p-1)%p=1,当a,p互素时。
然后用快速幂算出乘积取模就可以了。
#include <stdio.h>
#include <algorithm>
#define LL long long
#define MOD 1000000007
const int maxn = 200005;
LL a[maxn], f_mut[maxn], l_mut[maxn], v[maxn];
LL cnt[maxn];
LL quick_power ( LL val, LL n )
{
LL ret = 1;
while ( n )
{
if ( n & 1 )
ret = ret*val%MOD;
val = val*val%MOD;
n >>= 1;
}
return ret;
}
int main ( )
{
int n, k;
scanf ( "%d", &n );
for ( int i = 1; i <= n; i ++ )
scanf ( "%I64d", &a[i] );
std :: sort ( a+1, a+1+n );
k = 1;
cnt[1] = 1;
for ( int i = 2; i <= n; i ++ ) //统计出相同素数的个数
{
if ( a[i] == a[i-1] )
cnt[k] ++;
else
{
k ++;
cnt[k] = 1;
a[k] = a[i];
}
}
l_mut[k+1] = 1;
for ( int i = k; i >= 1; i -- )
l_mut[i] = l_mut[i+1]*( cnt[i]+1 )%( MOD-1 );
f_mut[0] = 1;
for ( int i = 1; i <= k; i ++ )
f_mut[i] = f_mut[i-1]*( cnt[i]+1 )%( MOD-1 );
for ( int i = 1; i <= k; i ++ )
//算出前后与当前素数组成的次方,并采用费马小定理对MOD-1进行取余
v[i] = ( 1+cnt[i] )*cnt[i]/2%( MOD-1 )*f_mut[i-1]%( MOD-1 )*l_mut[i+1]%( MOD-1 );
LL ans = 1;
for ( int i = 1; i <= k; i ++ )
ans = ans*quick_power ( a[i], v[i] )%MOD;
printf ( "%I64d", ans );
return 0;
}