Codeforces 615D Multipliers(數學推公式)

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


題意:題目很清楚,給你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
36
37
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);
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章