結果

問題 No.181 A↑↑N mod M
ユーザー 👑 kmjpkmjp
提出日時 2015-04-06 02:19:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 151 ms / 5,000 ms
コード長 1,566 bytes
コンパイル時間 1,241 ms
コンパイル使用メモリ 160,560 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-03 05:45:24
合計ジャッジ時間 8,376 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

ll A,N,M;

ll modpow(ll a, ll n, ll mo) {
	ll r=1;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

const int Mmax=2000;
int phi[Mmax+20];

// return min(upbound,A^^B)
ll AtB(ll a,ll b,ll upbound) {
	if(b==0 || a==1) return min(upbound,1LL);
	if(b==1) return min(upbound,a);
	if(b==2) return (a>10)?upbound:min(modpow(a,a,1LL<<60),upbound);
	if(a==2 && b==3) return min(upbound,1LL<<4);
	if(a==3 && b==3) return min(upbound,modpow(3,27,1LL<<60));
	if(a==2 && b==4) return min(upbound,1LL<<16);
	return upbound;
}

ll H4M(ll a,ll b,ll m) {
	if(m==1) return 0;
	if(a==1 || b==0) return 1;
	
	ll t=AtB(a,b-1,Mmax+1);
	if(t<=Mmax) return modpow(a,t,m);
	return modpow(a,Mmax+(H4M(a,b-1,phi[m]) - Mmax) % phi[m], m);
}

void GetPhi() {
	int i,x;
	for(x=1;x<=Mmax;x++) for(i=1;i<=x;i++) phi[x]+=__gcd(i,x)==1;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>A>>N>>M;
	GetPhi();
	cout<< H4M(A,N,M) << endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0