牛客训练:小a与黄金街道(欧拉函数+快速幂)

时间:2024-03-23 15:05:26

题目链接:传送门

思路:欧拉函数的性质:前n个数的欧拉函数之和为φ(n)*n/2,由此求出结果。

参考文章:传送门

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const LL MOD = 1e9+;
LL POW(LL a,LL b)
{
LL ans=;
while(b)
{
if(b%) ans=ans*a%MOD;
a=a*a%MOD;
b/=;
}
return ans;
}
LL Euler(LL x)
{
LL i,ans=x;
for(i=;i<=sqrt(x);i++)
{
if(x%i==)
{
ans=ans/i*(i-)%MOD;
while(x%i==) x/=i;
}
}
if(x>)
{
ans=ans/x*(x-)%MOD;
}
return ans;
}
int main(void)
{
LL n,k,A,B;
while(~scanf("%lld%lld%lld%lld",&n,&k,&A,&B))
{
LL ans=n*Euler(n)/; //注意:这里不能对MOD取余
ans=(A+B)%MOD*POW(k,ans)%MOD;
printf("%lld\n",ans);
}
return ;
}