# NOIP2012同余方程

3 10

7

## 备注

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
ll euler_phi(ll x){
ll m = (ll)sqrt(x+0.5);
ll ans = x;
for(int i = 2;i <= m;i++){
if(x % i == 0){
ans = ans / i * (i-1);
while(x % i == 0) x /=  i;
}
}
if(x > 1) ans = ans / x * (x-1);
return ans;
}
ll q_mul(ll a,ll b,ll p){
ll ans = 0;
while(b){
if(b&1){
ans = (ans + a) % p;
b--;
}
b >>= 1;
a = (a + a) % p;
}
return ans;
}
ll q_pow(ll a,ll b,ll p){
ll ans = 1;
while(b){
if(b&1){
ans = q_mul(ans,a,p);
}
b >>= 1;
a = q_mul(a,a,p);
}
return ans;
}
void exgcd(ll a,ll b,ll& d,ll& x,ll& y){
if(b == 0){
x = 1;
y = 0;
d = a;
return;
}
exgcd(b,a%b,d,y,x);
y -= (a/b)*x;
}
int phi[1000];
void phi_table(int n){
for(int i = 2;i <= n;i++) phi[i] = 0;
phi[1] = 1;
for(int i = 2;i <= n;i++) if(!phi[i])
for(int j = i;j <= n;j+=i){
if(!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i-1);
}
}
int main(){
ll a,b,d,x,y;
cin>>a>>b;
cout<<q_pow(a,euler_phi(b)-1,b);
return 0;
}

posted @ 2016-08-21 23:54  ACforever  阅读(141)  评论(0编辑  收藏  举报