题目:http://codeforces.com/problemset/problem/359/C
题意:给非增序列a[]和x,求的分子和分母的gcd。
思路:,显然,不过当最小的个数超过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;
}