cf C. Prime Number

时间:2023-03-09 06:37:15
cf C. Prime Number

http://codeforces.com/contest/359/problem/C

先求出分子的公因子,然后根据分子上会除以公因子会长生1,然后记录1的个数就可以。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 200000
#define LL __int64
using namespace std; LL a[maxn];
LL n,x;
LL min1(LL s,LL d)
{
if(s>d)return d;
return s;
} LL pow_mod(LL a,LL p,LL m)//快速幂取模
{
if(p==) return ;
LL ans1=pow_mod(a,p/,m);
ans1=ans1*ans1%m;
if(p%==) ans1=ans1*a%m;
return ans1;
} int main()
{
while(scanf("%I64d%I64d",&n,&x)!=EOF)
{
LL sum=;
for(int i=; i<=n; i++)
{
scanf("%I64d",&a[i]);
sum+=a[i];
}
LL ans=sum-a[n];//分子中最小公因子
LL c=a[n];
int t1=;//记录可以产生1的个数
int pos=n;
while(c>=)
{
while(a[pos]==c&&pos>=)
{
pos--;
t1++;
}
c--;
if(t1%x==)
{
ans++;
t1/=x;
}
else
break;
}
ans=min1(ans,sum);
printf("%I64d\n",pow_mod(x,ans,));
}
return ;
}