Codeforces 615D Multipliers 【组合数学】

来源:互联网 发布:淘宝女士运动套装 编辑:程序博客网 时间:2024/05/21 09:55

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.

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 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.



题意:给定m个质因子p[],有p[1]*p[2]*...*p[m] == n,问n所有因子的乘积 % 1e9 + 7。


思路:组合数学。

先把所有因子存起来,并统计个数为top(不相同的)。考虑每个质因子rec[i]做出的贡献,结果为rec[i]^num。

其中num为rec[i]在n所有因子乘积中存在的个数。

定义1-i质因子的组合方案数l[i],后i-top个质因子的组合方案数r[i]。

考虑第i个质因子的状态,取或不取,不取有l[i-1],取则有l[i-1] * cnt[rec[i]](可能取1 - cnt[rec[i]])

同理求法r[]。


对于一个质因子rec[i],可以与前、后组合,那么它的可组合方案数为temp = l[i-1] * r[i+1]。

若该质因子有Num个,则有方案rec[i] * temp 、rec[i] * rec[i] * temp... rec[i]^(Num) * temp。

统计rec[i]出现次数有num = (Num+1) * Num/2 * temp.下面ans *= rec[i] ^ num就好了。 

num值很大,费马小定理a^n = a^(n%(m-1)) (%m)搞下就ok了。也就是说num % (1e9+7-1)。


AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN (200000+10)#define MAXM (200000+10)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)using namespace std;LL pow_mod(LL a, LL n){    LL ans = 1;    while(n)    {        if(n & 1LL)            ans = ans * a % MOD;        a = a * a % MOD;        n >>= 1;    }    return ans;}LL l[MAXN], r[MAXN];LL cnt[MAXN];int rec[MAXN];int main(){    int m; Ri(m);    int top = 0;    for(int i = 1; i <= m; i++)    {        int a; Ri(a);        if(!cnt[a])            rec[++top] = a;        cnt[a]++;    }    l[0] = r[top+1] = 1;    for(int i = 1; i <= top; i++)        l[i] = l[i-1] * (cnt[rec[i]] + 1) % (MOD-1);    for(int i = top; i >= 1; i--)        r[i] = r[i+1] * (cnt[rec[i]] + 1) % (MOD-1);    LL ans = 1;    for(int i = 1; i <= top; i++)    {        LL Num = cnt[rec[i]];        if(Num)        {            LL num = l[i-1] * r[i+1] % (MOD-1);            num = (Num + 1) * Num / 2 % (MOD-1) * num % (MOD-1);            ans = ans * pow_mod(rec[i], num) % MOD;        }    }    Pl(ans);    return 0;}


0 0
原创粉丝点击