P5091 【模板】欧拉定理
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll qmul(ll a, ll b, ll mod) { 5 ll res = 0; 6 while (b) { 7 if (b&1) res = (res+a)%mod; 8 a = (a+a)%mod; 9 b >>= 1; 10 } 11 return res; 12 } 13 ll qpow(ll a, ll b, ll mod) { 14 ll res = 1; 15 while (b) { 16 if (b&1) res = qmul(res,a,mod); 17 a = qmul(a,a,mod); 18 b >>= 1; 19 } 20 return res; 21 } 22 ll phi(ll n) { 23 ll ans = n, m = sqrt(n); 24 for (ll i = 2; i <= m; i++) { 25 if (n % i == 0) { 26 ans = ans/i*(i-1); 27 while (n%i == 0) n /= i; 28 } 29 } 30 if (n > 1) ans = ans/n*(n-1); 31 return ans; 32 } 33 inline ll read(ll m) { 34 register ll x = 0, f = 0; char ch = getchar(); 35 while (!isdigit(ch)) ch = getchar(); 36 while (isdigit(ch)) { 37 x = x*10 + ch-'0'; 38 if (x >= m) f = 1; 39 x %= m; ch = getchar(); 40 } 41 return x + (f==1 ? m:0); 42 } 43 int main() { 44 ll a, m; scanf("%lld%lld",&a,&m); 45 ll b = read(phi(m)); 46 printf("%lld\n", qpow(a,b,m)); 47 return 0; 48 }