题目描述
给你三个正整数,a,m,b,你需要求:
a b m o d    m a^b \mod m abmodm
输入格式
一行三个整数,a,m,b
输出格式
一个整数表示答案
输入输出样例
输入 #1 复制
2 7 4
输出 #1 复制
2
输入 #2 复制
998244353 12345 98765472103312450233333333333
输出 #2 复制
5333
说明/提示
注意输入格式,a,m,b 依次代表的是底数、模数和次数
数据范围:
对于全部数据:
1 ≤ a ≤ 1 0 9 1≤a≤10^9 1≤a≤109
1 ≤ b ≤ 1 0 20000000 1≤b≤10^{20000000} 1≤b≤1020000000
1 ≤ m ≤ 1 0 6 1≤m≤10^6 1≤m≤106
#include <iostream>
#include <cstdio>
using namespace std;
int a,b,m,temp,phi,ans=1;
bool flag;
long long ok(long long a,long long b,long long mod){
long long ret=1%mod;
while(b){
if(b&1) ret=ret*a%mod;
a=a*a%mod;b>>=1;
}
return ret;
}
int main(){
int i;
char c;
scanf("%d%d",&a,&m);
temp=phi=m;
for (i=2;i*i<=m;++i){
if (temp%i==0){
phi=phi-phi/i;
while (temp%i==0){
temp/=i;
}
}
}
if (temp>1) phi=phi-phi/temp;
while(!isdigit(c=getchar()));
for (;isdigit(c);c=getchar()){
b=b*10+c-'0';
if(b>=phi){
flag=true;
b%=phi;
}
}
if (flag) b+=phi;
cout<<ok(a,b,m)<<endl;;
return 0;
}