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;
}