CodeForces - 359C-Prime Number

Simon has a prime number x and an array of non-negative integers a1, a2, ..., an.

Simon loves fractions very much. Today he wrote out number  on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: , where number t equals xa1 + a2 + ... + an. Now Simon wants to reduce the resulting fraction.

Help him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (109 + 7).

The first line contains two positive integers n and x (1 ≤ n ≤ 1052 ≤ x ≤ 109) — the size of the array and the prime number.

The second line contains n space-separated integers a1, a2, ..., an (0 ≤ a1 ≤ a2 ≤ ... ≤ an ≤ 109).

Print a single number — the answer to the problem modulo 1000000007 (109 + 7).

Examples

Input
2 2
2 2
Output
8
Input
3 3
1 2 3
Output
27
Input
2 2
29 29
Output
73741817
Input
4 5
0 0 0 0
Output
1

Note

In the first sample . Thus, the answer to the problem is 8.

In the second sample, . The answer to the problem is 27, as 351 = 13·27, 729 = 27·27.

In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.

In the fourth sample . Thus, the answer to the problem is 1.

思路是主要的进制的思想

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define mod 1000000007

const int maxn=1e5+5;
typedef long long ll;
using namespace std;
ll quickpow(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
        ans=(ans*a)%mod;
        b>>=1;
        a=(a*a)%mod;
    }
    return ans;
}
ll a[maxn];
int main()
{
    ll n,x;
    cin>>n>>x;
    ll s=0;
    for(int t=0;t<n;t++)
    {
        scanf("%lld",&a[t]);
        s+=a[t];
    }
    for(int t=0;t<n;t++)
    {
        a[t]=s-a[t];
    }
    ll ans,cnt=1;
    sort(a,a+n);
    a[n]=-1;
    for(int t=1;t<=n;t++)
    {
        if(a[t]!=a[t-1])
        {
            if(cnt%x)
            {
                ans=a[t-1];
                break;
            }
            else
            {
                cnt/=x;
                a[t-1]++;
                t--;
            }
        }
        else
        {
            cnt++;
        }
    }
    ll ss=min(s,ans);
    cout<<quickpow(x,ss)<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Staceyacm/p/10800703.html