为什么要降幂:
什么是欧拉降幂:
注: φ(c)是欧拉函数;
欧拉降幂怎么实现:
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int MAX=1000100;
//快速幂
ll fastPow(ll a,ll b,ll mod) {
ll ans=1;
a %= mod;
while(b) {
if(b&1) {
ans = (ans*a)%mod;
}
b >>= 1;
a = (a*a)%mod;
}
return ans;
}
//欧拉函数
ll eulerFunction(ll x) {
ll eulerNumbers = x;
for(ll i = 2; i*i <= x; i++) {
if(x % i == 0) {
eulerNumbers = eulerNumbers / i * (i-1);
while(x % i == 0) {
x /= i;
}
}
}
if(x > 1) {
eulerNumbers = eulerNumbers / x * (x-1);
}
return eulerNumbers;
}
//降幂函数
ll eulerDropPow(ll a,char b[],ll c) {
ll eulerNumbers = eulerFunction(c);
//存储降了之后的幂
ll descendingPower=0;
for(ll i=0,len = strlen(b); i<len; ++i) {
descendingPower=(descendingPower*10+b[i]-'0') % eulerNumbers;
}
//b mod &(c) + &(c)
descendingPower += eulerNumbers;
return fastPow(a,descendingPower,c);
}
int main() {
ll a,c;
char b[MAX];//降幂函数,因为幂方的值可能巨大,故用char数组储存
while(~scanf("%lld%s%lld",&a,b,&c)) { // a 的 b 次方对c取模
printf("%lld\n",eulerDropPow(a,b,c));
}
return 0;
}