您的位置:首页 > 其它

Prime Number(CodeForces - 359C )

2017-08-30 15:41 2506 查看
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 ≤ 105, 2 ≤ 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·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.

此题是求分子与分母的最小公倍数,分母是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));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数论