Prime Number(CodeForces - 359C )

C. Prime Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Input

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

Output

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·27729 = 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.

此题是求分子与分母的最小公倍数,分母是x的(a1+a2+a3......aN)次方,假设a1 + a2 + a3....+aN的和为sum,而分子是x^(sum-a1)+x^(sum -a2)

+x^(sum-a3)....+x(sum-an),那么分子和分母的最小公倍数一定是,x的k次幂,那么k就是sum-an中最小的,还有需要注意的是,如果有多个sum-an

相同,那么每当出现底数x个时,k+1,最后找出最小的即可

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
long long y[1000000];
struct s{
   long long  x;
   friend bool operator <(s u,s v)
   {
       return u.x>v.x;
   }
};
long long p(long long x,long long y)
{
    const long long  mod=1e9+7;
    long long ans=1;
    long long tmp=x;
    while(y)
    {
        if(y%2==1)
            ans=(ans*tmp)%mod;
        tmp=(tmp*tmp)%mod;
        y/=2;
    }
    return ans;
}
long long  b[1000000];
int main()
{
    long long n,m;
    scanf("%I64d%I64d",&n,&m);
    long long sum=0;
    for(int i=0;i<n;i++)
    {
        scanf("%I64d",&y[i]);
        sum+=y[i];
    }
    priority_queue<s>q;
    for(int i=0;i<n;i++)
    {
        b[i]=sum-y[i];
        s tmp;
        tmp.x=b[i];
        q.push(tmp);
    }
    long long cnt=0;
    long long tm=q.top().x;
    while(!q.empty())
    {
        s temp=q.top();
        if(temp.x==tm)
        {
            cnt++;
            q.pop();
            if(cnt==m)
            {
                s tn;
                cnt=0;
                tn.x=tm+1;
                q.push(tn);
                tm=q.top().x;
            }
        }
        else
        {
            break;
        }
    }
    long long ans=min(tm,sum);
    printf("%I64d\n",p(m,ans));
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值