题面
题解
比赛的时候是别人做的,所以赛后补题。
题意就是求塔幂,共b个a求幂,并对m取模,显然是欧拉降幂
当然还是有坑点的,塔幂调了好久的bug。或者说是自己智障了。。
代码
// #include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <vector>
#include <assert.h>
#include <cmath>
#include <ctime>
using namespace std;
#define me(x,y) memset((x),(y),sizeof (x))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define SGN(x) ((x)>0?1:((x)<0?-1:0))
#define ABS(x) ((x)>0?(x):-(x))
// #define int __int128
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn = 1e6+10;
const int inf = __INT32_MAX__;
const ll INF = __LONG_LONG_MAX__;
const int MOD = 1e9+7;
const double eps = 1e-10;
const double PI = std::acos(-1);
ll phi[maxn],prime[maxn],vis[maxn],tot;
void init(){
phi[1] = vis[1] = 1;
for(int i = 2; i < maxn; ++i){
if(!vis[i]) prime[++tot] = i,phi[i] = i-1;
for(int j = 1; j <= tot && i*prime[j] < maxn; ++j){
vis[i*prime[j]] = 1;
if(i%prime[j] == 0){
phi[i*prime[j]] = phi[i]*prime[j];
break;
}
phi[i*prime[j]] = phi[i]*phi[prime[j]];
}
}
}
ll check(ll x,ll p){
return x < p ? x : (x%p+p);
}
ll qpow(ll a,ll b,ll p){
ll ans = 1;
while(b){ if(b&1) ans = check(ans*a,p); a = check(a*a,p); b >>=1;}
return ans;
}
ll a,b,m;
ll dfs(ll i,ll p){
if(i == b || p == 1) return check(a,p);
return qpow(a,dfs(i+1,phi[p]),p);
}
int main(){
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("1in.in","r",stdin);
freopen("1out.out","w",stdout);
#endif
init();
ll t;
cin>>t;
while(t--){
cin>>a>>b>>m;
if(b == 0) {cout<<(1%m)<<endl;continue;}
cout<<dfs(1ll,m)%m<<endl;
}
return 0;
}