Prime Number

Simon has a prime number x and an array of non-negative integersa1, 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 numbert equals xa1 + a2 + ... + an. Now Simon wants to reduce the resulting fraction.

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

Input

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

The second line contains n space-separated integersa1, 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是素数,所以其实就是要将分子和分母都表示成(x^k)*m,m不能被x整除,然后取分子和分母k的最小值。

分母已经固定的为  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<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
long long int a[100100];
long long int s[100100];
long long qp(long long int n,long long int k)//快速幂
{
    long long int ret = 1;
    while(k)
    {
        if(k&1)
            ret = (ret*n) % 1000000007;
        k = k>>1;
        n = n*n % 1000000007;
    }
    return ret;
}
int main()
{
    long long int n,m;
    while(scanf("%lld%lld",&n,&m)!=-1)
    {
        long long int sum=0;
        memset(s,0,sizeof(s));
        for(long long int i=1; i<=n; i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
        }
        long long int ans=sum-a[n];//得出最小的幂,从分子最小的幂开始求公约数,把它当做公约数
        for(long long int i=1; i<=n; i++)//通分后,把分子每一个幂都求出来,存起来
        {
            s[i]=sum-a[i];
        }
        int l=0;
        long long int ha;
        long long int we=ans;
        for(long long int i=n; i>=1; i--)//从分子最小的幂开始ans,在数组中找和它相等幂的个数,如果个数l能整除m,最小则ans+1,,然后找ans+1之后的,在数组中找和它相等的幂的个数,看l是否整除m,如果可以就重复,不可以就跳出循环,此时的ans就是最大的,则qp(m,ans)就是最大的公约数。
        {
            i=n;
                if(s[i]==we)
                {
                    ha=we;//用we标记当前的幂,方便在数组里面找和这个幂相等的幂的个数
                    while(n&&s[n]==ha)
                    {
                        n--;
                        l++;
                    }
                    if(l%m)break;//l(就是系数)不能整除m则跳出,此时的ans就是所求
                    ans+=1;//能整除,则幂就+1,此时的系数就是l/m
                    we=ans;
                    l=l/m;
                }
                else//防止在数组中没找到和当前幂相等的数,则就看当前幂的数的系数是不是能整除m,能就ans+1,不能就跳出循环。像3*3^4,如果数组里面没有3^4则,,则此时就把它写成3^5,,,再在数组里面找和3^5有相同幂的数的个数
                {
                    if(l%m)break;
                    l=l/m;
                    ans+=1;
                    we=ans;
                }
           }
          while(l%m==0)//防止l还可以整除m,例如10*10^2,,,则可以写成10^3,则qp(m,3)就是最大的公约数,如果没这步的话,ans就是2了,最大公约数就会变小
          {
              l/=m;
              ans++;
          }
        long long int ta;
        ta=min(ans,sum);//取最小的幂,因为有可能分子大于分母,此时取分母为最大公约数
        //printf("%d\n",ta);
        printf("%lld\n",qp(m,ta));
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值