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

题目链接:传送门

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

参考文章:传送门

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const LL MOD = 1e9+7;
LL POW(LL a,LL b)
{
    LL ans=1;
    while(b)
    {
        if(b%2) ans=ans*a%MOD;
        a=a*a%MOD;
        b/=2;
    }
    return ans;
}
LL Euler(LL x)
{
    LL i,ans=x;
    for(i=2;i<=sqrt(x);i++)
    {
        if(x%i==0)
        {
            ans=ans/i*(i-1)%MOD;
            while(x%i==0) x/=i;
        }
    }
    if(x>1)
    {
        ans=ans/x*(x-1)%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)/2; //注意:这里不能对MOD取余 
        ans=(A+B)%MOD*POW(k,ans)%MOD;
        printf("%lld
",ans);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/2018zxy/p/10313845.html