2019icpc 南京网络赛b
https://nanti.jisuanke.com/t/41299
指数循环节,快速幂递归求解,需要注意的是快速幂乘的时候如果超过了m需要+m补全循环节
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+5; const ll inf=2147483647; inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f; } ll eular(ll n) { ll ans = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { ans -= ans / i; while (n % i == 0) n /= i; } } if (n != 1) ans -=ans / n; return ans; } inline ll md(ll a,ll m) { return a>=m ? a%m+m:a; //超过m就+m补全循环节 } int quick_mi(ll a, ll k,ll p) { ll ans = 1; while (k) { if (k & 1) ans=md(ans*a,p); a=md(a*a,p); k >>= 1; } return ans; } int dfs(int x,int y,int p) { if (y == 0) return 1; if (y == 1) return x; if (p == 1) return 1; ll ans=quick_mi(x,dfs(x,y-1,eular(p)),p); return ans; } int main() { int t; t=read(); while(t--) { int a,b,m; a=read(),b=read(),m=read(); int ans=dfs(a,b,m); cout<<ans%m<<endl; } return 0; }