您的位置:首页 > 其它

Codeforces 615D Multipliers(数学推公式)

2016-04-05 08:46 441 查看
escription

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

Input
2
2 3


Output
36


Input
3
2 3 2


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

题意:题目很清楚,给你n个质因数,求他们的积的所有因子之积(包括1和本身)。

(看到题目很懵圈~想着要是知道所有因子该有多好~(那还做个P的题目))。

思路:(我也是看题解的,简单翻译一下吧.)

题目已给n个质因数,这个条件好好哦,如果不是,那还得自己求质因数~.我们设n个质因数之积为x,那么x的因子个数必定是由这n个质因数两两乘或者是一个数本身.直接给出公式:x=p1^a1*p2^a2*p3^a3*.......*pn^an,

那么设d(x)为x的因子个数:d(x)=(a1+1)*(a2+1)*(a3+1)*...*(an+1);

(这个公式可以这样理解:有a1个p1,那么可以选0个,1个,2个...a1个,所以有(a1+1)。)

x其中的一个因子m,那么也就有x/m这个因子的存在。那么就有d(x)/2对这样的因子,

所以设f(x)为最终答案,给出公式f(x)=x^(d(x)/2)。

那么要是x是个完全平方数,d(x)不是个奇数吗?怎么办?要多乘一个sqrt(x).?

其实并不用。

因为x很大,d(x)/2也很大,我们并不能直接求取。

那么就要拆开取模,怎么拆啊?d(ab)=d(a)*d(b),f(ab)=f(a)^d(b)*f(b)^d(a)。

所以对于一个x是完全平方数,就有公式f(p^k)=p^(k*(k+1)/2),可以直接得到,所以不用多乘sqrt(x)(将fx公式带入得)。

拆完x,那指数d(x)/2很大咋办?那么由费马小定理得:

,所以指数只能对(mod-1)取模。(因为它处在指数位置,所以不能随便对mod直接取模)。


真不容易~

附上AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
3637
38
39
40
41

#include<stdio.h>
#include<math.h>
#include<string.h>
#define max(a,b) a>b?a:b
#include<map>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
long long quickpow(long long n,long long m)
{
long long ans=1;
while(m>0)
{
if(m&1)ans=ans*n%mod;
m>>=1;
n=n*n%mod;
}
return ans;
}
int main()
{
int n,a,Max;
while(~scanf("%d",&n))
{
map<ll ,ll >p;
map<ll ,ll >::iterator it;
for(int i=0; i<n; i++)
{
scanf("%d",&a);
p[a]++;
}
long long d=1,ans=1,fp;
for(it=p.begin(); it!=p.end(); it++)
{
fp=quickpow(it->first,(it->second+1)*it->second/2);
ans=quickpow(ans,(it->second+1))*quickpow(fp,d)%mod;
d=d*(it->second+1)%(mod-1);
}
printf("%I64d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: