模板 欧拉定理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<string> #include<cmath> #include<cstring> #include<vector> #include<map> #include<set> #include<algorithm> #include<queue> #include<stack> #include<list> #include<sstream> #include<cstdio> #define INF 0x3f3f3f3f const int maxn = 1e6 + 5; const double PI = acos(-1.0); typedef long long ll; typedef unsigned long long ull; using namespace std; ll a, b, m; inline ll read(ll m) { register ll x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) { x = x * 10 + ch - '0'; if (x >= m) f = 1; x %= m, ch = getchar(); } return x + (f == 1 ? m : 0); } ll euler_phi(ll n) { ll m = ll(sqrt(n + 0.5)); ll ans = n; for (int i = 2; i <= m; i++) { if (n % i == 0) { ans = ans / i * (i - 1); while (n % i == 0) n /= i; } } if (n > 1) ans = ans / n * (n - 1); return ans; } ll quickPower(ll a, ll b, ll m) { //计算a的b次方 ll ans = 1; ll base = a; while (b) { if (b & 1) { ans *= base; ans %= m; } base *= base; base %= m; b >>= 1; //注意是b>>=1 not b>>1 } return ans; } int main() { scanf("%lld%lld", &a, &m); b = read(euler_phi(m)); printf("%lld\n", quickPower(a, b, m)); return 0; }