CodeForces - 359C

xiaoxiao2021-02-27  120

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

Example 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^s s= (a1+a2+a3+...),所以最主要的就是处理分子了,我们知道分子上k的最小值一定是相加时分母的最大值。

首先对于每个1/x^ai 通分之后分子变为x^(s-ai),我们的任务就是要求出分子中的最大公约数就是我们所求的值,肯定就是次数最小的那一项了;

但是有一个特殊情况我们需要考虑,就是有些项相加的系数可能会被x整除,所以我们还要对分子进行处理;

例如分子为2^2+2^2=2*2^2=2^3,最小的k为3,不是2

处理方法:

   我们可以知道的是,由于输入的ai是按照非递减的顺序输入,s=a1+a2+a3+...,所以初始最小的k应为 s-an;(即 1/x^an 分母变为 x^s 需要乘上x^s-an 为最小);

然后我们就需要开始找相同项的次数加在一块能否整除x;我们这里从最小项an开始,因为只有最小的项加起来的次数可以整除x才能改变最小的k值,最小的没有,最大的对k的值没有影响,)每次取最小的值t的系数,判断能否被x整除,如果不可以这个t就直接是我们要找的k,如果可以整除那么我们就乘一个x到x^t上去 使其变为x^(t+1),也就是将count/x的系数加到x^(t+1)次方上去,知道找到一个最小的t就是我们要找的k了.

#include <cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<string> #define min(a,b) (a<b?a:b) using namespace std; typedef long long ll; /* 还是因为数据的问题,中间的运算又超出了范围,导致溢出 我们以后看见数字的题,我们所有的变量同一long long */ const int maxn=1e5+10; ll n,x; ll val[maxn]; const int mod=1e9+7; ll quick_pow(ll x,ll n) { ll ans=1; x=x%mod; while(n){ if(n&1) ans=ans*x%mod; x=x*x%mod; n>>=1; } return ans; } int main() { scanf("%I64d %I64d",&n,&x); ll sum=0; for(int i=0;i<n;i++){ scanf("%I64d",&val[i]); sum += val[i]; } for(int i=0;i<n;i++) val[i]=sum-val[i]; sort(val,val+n); ll cnt=1; val[n]=-1; ll min_num; for(int i=1;i<=n;i++){ if(val[i]==val[i-1]) cnt++; else{ if(cnt%x){ min_num=val[i-1]; break; } else{ val[i-1]++; cnt/=x; i--;//返回上一个点,看看前面 } } } ll ans_num=min(min_num,sum); printf("%I64d\n",quick_pow(x,ans_num)); return 0; }
转载请注明原文地址: https://www.6miu.com/read-15630.html

最新回复(0)