Codeforces 209 div2 C. Prime Number

題目:C. Prime Number

思路:通分之後,分母是x^sum,分子是sigma(x^(sum-a[i])),首先能提取的公因式是min(x^(sum-a[i])),對於剩下的,看產生的1能不能形成ax的形式,while搞定

 tag :暴力


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long LL;
LL mod=LL(1e9+7);
#define maxn 100010
int num[maxn];
LL Pow(LL a,LL b)
{
    LL ans=1;
    while(b)
    {
        if(b&1)
        {
            b--;
            ans=(ans*a)%mod;
        }
        else
        {
            b/=2;
            a=(a*a)%mod;
        }
    }
    return ans;
}
int main()
{
    LL n,x;
    scanf("%I64d%I64d",&n,&x);
    LL sum=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&num[i]);
        sum+=num[i];
    }
    int index=n;
    long long ans=sum-num[n];
    int p=num[n];
    int tmp=0;
    while(p>=1)
    {
        while(num[index]==p && index>=1)
        {
            index--;
            tmp++;
        }
        p--;
        if(tmp%x==0)
        {
            ans++;
            tmp/=x;
        }
        else
            break;
    }
    ans=min(ans,sum);
    printf("%I64d\n",Pow(x,ans));
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章