cf C. Prime Number
http://codeforces.com/contest/359/problem/C
先求出分子的公因子,然后根据分子上会除以公因子会长生1,然后记录1的个数就可以。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 200000 5 #define LL __int64 6 using namespace std; 7 8 LL a[maxn]; 9 LL n,x; 10 LL min1(LL s,LL d) 11 { 12 if(s>d)return d; 13 return s; 14 } 15 16 LL pow_mod(LL a,LL p,LL m)//快速幂取模 17 { 18 if(p==0) return 1; 19 LL ans1=pow_mod(a,p/2,m); 20 ans1=ans1*ans1%m; 21 if(p%2==1) ans1=ans1*a%m; 22 return ans1; 23 } 24 25 int main() 26 { 27 while(scanf("%I64d%I64d",&n,&x)!=EOF) 28 { 29 LL sum=0; 30 for(int i=1; i<=n; i++) 31 { 32 scanf("%I64d",&a[i]); 33 sum+=a[i]; 34 } 35 LL ans=sum-a[n];//分子中最小公因子 36 LL c=a[n]; 37 int t1=0;//记录可以产生1的个数 38 int pos=n; 39 while(c>=1) 40 { 41 while(a[pos]==c&&pos>=1) 42 { 43 pos--; 44 t1++; 45 } 46 c--; 47 if(t1%x==0) 48 { 49 ans++; 50 t1/=x; 51 } 52 else 53 break; 54 } 55 ans=min1(ans,sum); 56 printf("%I64d\n",pow_mod(x,ans,1000000007)); 57 } 58 return 0; 59 }