题意:给定质数x,有n个1/(x^ai) (1<=i<=n)相加,分母为x^(a1 + a2 + a3 + …… + an),分子为T。求分子,分母最大公因数。
思路:设分母为:dwn,x^p的个数为mp[p]。分子为 x^(a1 + a2 …… + an - ai)相加。因为an最大,所以分子第n项最小。提出该项dwn / (x^an) == x((分母次数) - an),设总次数为(分母次数 - div)如果该项系数(个数)% x == 0,则mp[div - 1+=mp[div] / x;div--;
#include <bits/stdc++.h> #include <unordered_map> using namespace std; #define N 500010 #define maxn (N + 10) #define Max(a,b) (a > b ? a : b) #define Min(a,b) (a < b ? a : b) typedef long long ll; const ll INF = (1e9) + 10; const ll mod = (1e9) + 7; unordered_map <int,int> mp; ll f_pow(ll x,ll p){ ll ans = 1; while(p){ if(p % 2 == 1){ ans *= x;ans %= mod; } x *= x;x %= mod;p /= 2; } return ans; } int main() { int n, x;scanf("%d%d",&n, &x); int an = 0; ll sum = 0; for(int i = 1;i <= n;i++){ int a;scanf("%d",&a); mp[a]++;sum += a; if(i == n)an = a; } int div = an; while(mp[div] % x == 0 && div >= 0){ mp[div - 1] += (mp[div] / x);div--; } if(div < 0)div = 0; sum -= div; //printf("sum = %lld ",sum); ll ans = f_pow(x,sum); printf("%lld ",ans); return 0; }