結果
| 問題 | 
                            No.181 A↑↑N mod M
                             | 
                    
| コンテスト | |
| ユーザー | 
                             beet
                         | 
                    
| 提出日時 | 2018-11-14 13:44:23 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 3 ms / 5,000 ms | 
| コード長 | 1,238 bytes | 
| コンパイル時間 | 1,813 ms | 
| コンパイル使用メモリ | 201,332 KB | 
| 最終ジャッジ日時 | 2025-01-06 16:38:48 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 6 | 
| other | AC * 37 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
Int phi(Int n){
  Int res=n;
  for(Int i=2;i*i<=n;i++){
    if(n%i==0){
      res=res/i*(i-1);
      for(;n%i==0;n/=i);
    }
  }
  if(n!=1) res=res/n*(n-1);
  return res;
}
Int peri(Int a,Int m){
  map<Int, Int> dp;
  dp[1]=0;
  for(Int i=1,x=1;i<=m;i++){
    x=x*a%m;
    if(dp.count(x)) return i-dp[x];
    dp[x]=i;
  }
  return -1;
}
Int mpow(Int x,Int n,Int m){
  Int v=1;
  while(n){
    if(n&1) v=v*x%m;
    x=x*x%m;
    n>>=1;
  }
  return v;
}
//INSERT ABOVE HERE
using P = pair<Int, Int>;
const Int MAX = 2020;
Int tetr(Int a,Int n){
  if(n==0) return 1;  
  if(a==1) return 1;
  Int v=1;
  for(Int i=0;i<n;i++){
    Int u=1;
    for(Int j=0;j<v;j++){
      u*=a;
      if(u>MAX) return -1;
    }
    v=u;
  }
  return v;
}
Int dfs(Int a,Int n,Int m){
  if(m==1) return 0;
  if(n==0) return 1;
  Int x=tetr(a,n-1);
  if(~x) return mpow(a,x,m);
  Int v=peri(a,m);
  return mpow(a,MAX+((dfs(a,n-1,v)-MAX)%v+v)%v,m);
}
signed main(){
  Int a,n,m;
  cin>>a>>n>>m;
  cout<<dfs(a,n,m)<<endl;
  return 0;
}
            
            
            
        
            
beet