Codeforces Round #338 (Div. 2)-D. Multipliers

来源:互联网 发布:搞笑网络神曲排行榜 编辑:程序博客网 时间:2024/05/29 11:35

原题链接

D. Multipliers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
22 3
output
36
input
32 3 2
output
1728
Note

In the first sample n = 2·3 = 6. The divisors of 6 are 123 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 12346 and 121·2·3·4·6·12 = 1728.


因为所有因子都是要乘起来,所以算出每个质因子(以及它的幂)乘了几次。先预处理出每个质因子出现的次数, num[i]表示i这个质因子出现了num[i]次,那么要算j这个质因子乘的次数 p = (num[k] + 1)(k为所有质因子除了j)的乘积.

那么接下来要算j^p % MOD, 因为p会很大所以根据费马小定理j^p%MOD = j^(p%(MOD-1))%MOD;

#include <cstdio>#include <cstring>#include <algorithm>#include <stack>#include <iostream>#include <vector>#include <queue>#include <cmath>#define maxn 200005#define INF 1e15 #define MOD 1000000007typedef long long ll;using namespace std;int num[maxn];ll k1[maxn], k2[maxn];ll pow_mod(ll p, ll m){ll ans = 1;while(m){if(m&1)(ans *= p) %= MOD;    (p *= p) %= MOD;    m >>= 1;}return ans;}int main(){   // freopen("in.txt", "r", stdin);int m, a, maxs = 0;scanf("%d", &m);for(int i = 0; i < m; i++){scanf("%d", &a);maxs = max(maxs, a);num[a]++;}k1[1] = 1;for(int i = 2; i <= maxs; i++){if(num[i])k1[i] = k1[i-1] * (num[i] + 1)% (MOD - 1);else   k1[i] = k1[i-1];}k2[maxs+1] = 1;for(int i = maxs; i >= 2; i--){if(num[i]) k2[i] = k2[i+1] * (num[i] + 1) % (MOD - 1);else k2[i] = k2[i+1];}ll ans = 1;for(int i = 2; i <= maxs; i++){if(num[i]){ll p1 = k1[i-1] * k2[i+1] % (MOD - 1), p2 = 1;for(int j = 1; j <= num[i]; j++){(p2 *= i) %= MOD;(ans *= pow_mod(p2, p1)) %= MOD;}}}printf("%I64d\n", ans);return 0;}


0 0
原创粉丝点击