#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define BET(a,b,c) ((a)<=(b)&&(b)<(c)) #define FOR(i,n) for(int i=0,i##_end=(int(n));i VI; typedef vector VVI; typedef long long ll_t; ll_t modpow(ll_t x , ll_t y , ll_t mod){ x %= mod; ll_t t = x, res = 1; for(; y ; y >>= 1){ if(y & 1) { res = res * t; if(res >= mod) res %= mod; } t = t * t; if(t >= mod) t %= mod; } return res; } vector g_prime; const int MAX_PRIME=5000; bool is_prime_[(MAX_PRIME+1)/2] ; bool is_prime(ll_t x){ if(x==2) return true; if(x<=1 || x%2==0) return false; return is_prime_[x/2]; } void setup_prime() { fill_n(is_prime_ , (MAX_PRIME+1)/2 , true); g_prime.clear() ; g_prime.push_back(2); for(int i=3;i<=MAX_PRIME;i+=2){ if(!is_prime_[i>>1]) continue; g_prime.push_back(i); for(int j=i+i+i;j<=MAX_PRIME;j+=i+i){ is_prime_[j>>1]=false; } } } int MaxPower = 5000; ll_t calcSmall(ll_t A, ll_t N, ll_t limit){ if(A == 0) return 0; if(A == 1) return 1; if(N == 1) return min(A, limit); if(N == 2){ // A ^ A if(A <= 10) return min(modpow(A, A, 1LL<<60), limit); } if(N == 3){ // A ^ (A ^ A) if(A == 2) return min(limit, 1LL<<16); } return limit; } ll_t phi(ll_t x){ ll_t val = x; for(int i=0;i<(int)g_prime.size();i++){ if(g_prime[i] > x) break; if(x % g_prime[i] == 0){ val = val / g_prime[i] * (g_prime[i] - 1) ; while(x % g_prime[i] == 0) x /= g_prime[i]; } } if(x>1){ val = val / x * (x - 1) ; } return val; } // A^A^A .. (x n) % mod ll_t solve(ll_t A, ll_t N, ll_t M){ if(N == 0) return 1; if(M == 1) return 0; if(A == 0) return 0; if(A == 1) return 1; int mmax = 1000; int small = calcSmall(A, N - 1, mmax + 1); if(small <= mmax){ return modpow(A, small, M); } int cycle = phi(M); return modpow(A, mmax + (solve(A, N - 1, cycle) - mmax) % cycle, M); } int main() { setup_prime(); int A,N,M; cin>>A>>N>>M; cout<