codeforces-359-C:Prime Number

题目:http://codeforces.com/problemset/problem/359/C

题意:给非增序列a[]和x,求\sum 1/x^{a_{i}}的分子和分母的gcd。

思路:\sum_{i=1}^{n} \frac{1}{x^{a_{i}}} = \frac{\sum_{i=1}^{n}x^{s-a_{i}}}{x^{s}}\cdot \cdot \cdot \cdot \cdot \cdot \cdot \cdot(s=\sum_{i=1}^{n}a_{i}),显然gcd = x^\min_{i=1}^{n}(s-a_{i}),不过当最小的个数超过x时,会对x+1造成一个贡献。当最小的个数能被x整除时,需要再向更大的考虑。注意分子有可能大于分母,例如样例8 2, 2 2 2 2 2 2 2 2,ans = 65536。

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e5+5;
const LL mod = 1e9+7;

LL quick_pow(LL a, LL b){
    LL sum = 1;
    while(b){
        if(b&1) sum = sum*a%mod;
        a = a*a%mod; b>>=1;
    }return sum;
}
LL n, x, sum, a[maxn];
int main()
{
    cin >> n >> x;
    for(int i=1; i<=n; i++) scanf("%lld", &a[i]), sum += a[i];
    for(int i=1; i<=n; i++) a[i] = sum-a[i];
    LL p, num = 1;
    for(int i=n-1; i>=0; i--){
        if(a[i] == a[i+1]) num ++;
        else {
            if(num % x == 0) num = num/x, a[i+1]++, i++;
            else {p = a[i+1]; break;}
        }
    }
    cout << quick_pow(x, min(p,sum)) << endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值