結果

問題 No.181 A↑↑N mod M
ユーザー beetbeet
提出日時 2019-12-17 20:20:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,472 bytes
コンパイル時間 2,414 ms
コンパイル使用メモリ 203,372 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-17 19:03:47
合計ジャッジ時間 4,273 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename T>
T totient(T n){
  T res=n;
  for(T 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;
}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#undef call_from_test

#endif
//BEGIN CUT HERE
template<typename T>
T mpow(T a,T n,T m,T &f){
  using ull = unsigned long long;
  if(a==1||n==0) return 1;
  ull v=1,x=a,z=m;
  f|=x>=z;
  x%=m;
  while(1){
    if(n&1) v*=x;
    if(v>=z) f=1,v%=m;
    n>>=1;
    if(!n) break;
    x=x*x;
    if(x>=z) f=1,x%=m;
  }
  return v;
}

template<typename T>
T tetration(T a,T n,T m,T &f){
  if(a==0) return ~n&1;
  if(m==1) return f=1;
  if(a==1||n==0) return 1;
  if(n==1){
    f|=a>=m;
    return a%m+f*m;
  }
  T z=tetration(a,n-1,totient(m),f);
  T r=mpow(a,z,m,f);
  return r+f*m;
}

template<typename T>
T tetration(T a,T n,T m){
  T f=0;
  return tetration(a,n,m,f)%m;
}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE

signed YUKI_181(){
  int a,n,m;
  cin>>a>>n>>m;
  cout<<tetration(a,n,m)<<endl;
  return 0;
}
/*
  verified on 2019/10/08
  https://yukicoder.me/problems/no/181
*/

signed SUMMERFES2018_F(){
  using ll = long long;

  ll a,m,k;
  cin>>a>>m>>k;
  ll ans=0;

  k--;
  for(ll i=0,p=-1;i<k;i++){
    ll x=tetration(a,i,m);
    if(p==x){
      ans+=(k-i)%m*x%m;
      ans%=m;
      break;
    }
    ans+=x;
    ans%=m;
    p=x;
  }
  cout<<ans<<endl;
  return 0;
}
/*
  verified on 2019/10/08
  https://atcoder.jp/contests/summerfes2018-div1/tasks/summerfes2018_f
*/

signed SPOJ_POWTOW(){
  using ll = long long;

  int T;
  cin>>T;
  while(T--){
    ll a,n,f=0;
    cin>>a>>n;
    stringstream ss;
    ss<<tetration(a,n,ll(1e9),f)%ll(1e9);
    auto s=ss.str();
    if(f){
      while(s.size()<9u) s='0'+s;
      cout<<"..."<<s<<endl;
    }else{
      cout<<s<<endl;
    }
  }
  return 0;
}
/*
  verified on 2019/10/08
  https://www.spoj.com/problems/POWTOW/
*/

signed SPOJ_MTETRA(){
  using ll = long long;
  int T;
  cin>>T;
  while(T--){
    ll a,n,m;
    cin>>a>>n>>m;
    cout<<tetration(a,n,m)<<endl;;
  }
  return 0;
}
/*
  verified on 2019/10/08
  https://www.spoj.com/problems/MTETRA/
*/

signed main(){
  YUKI_181();
  //SUMMERFES2018_F();
  //SPOJ_POWTOW();
  //SPOJ_MTETRA();
}
#endif
0