結果

問題 No.181 A↑↑N mod M
ユーザー 👑 kmjpkmjp
提出日時 2015-04-06 01:21:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 70 ms / 5,000 ms
コード長 1,607 bytes
コンパイル時間 3,241 ms
コンパイル使用メモリ 145,860 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 18:54:36
合計ジャッジ時間 5,702 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
4,380 KB
testcase_01 AC 70 ms
4,380 KB
testcase_02 AC 70 ms
4,376 KB
testcase_03 AC 70 ms
4,376 KB
testcase_04 AC 70 ms
4,384 KB
testcase_05 AC 70 ms
4,380 KB
testcase_06 AC 70 ms
4,380 KB
testcase_07 AC 70 ms
4,384 KB
testcase_08 AC 70 ms
4,380 KB
testcase_09 AC 70 ms
4,380 KB
testcase_10 AC 70 ms
4,376 KB
testcase_11 AC 70 ms
4,376 KB
testcase_12 AC 70 ms
4,380 KB
testcase_13 AC 70 ms
4,376 KB
testcase_14 AC 70 ms
4,376 KB
testcase_15 AC 70 ms
4,380 KB
testcase_16 AC 70 ms
4,380 KB
testcase_17 AC 70 ms
4,380 KB
testcase_18 AC 70 ms
4,380 KB
testcase_19 AC 70 ms
4,380 KB
testcase_20 AC 70 ms
4,380 KB
testcase_21 AC 70 ms
4,380 KB
testcase_22 AC 70 ms
4,376 KB
testcase_23 AC 70 ms
4,380 KB
testcase_24 AC 70 ms
4,384 KB
testcase_25 AC 70 ms
4,380 KB
testcase_26 AC 70 ms
4,380 KB
testcase_27 AC 70 ms
4,380 KB
testcase_28 AC 70 ms
4,376 KB
testcase_29 AC 70 ms
4,380 KB
testcase_30 AC 70 ms
4,384 KB
testcase_31 AC 70 ms
4,384 KB
testcase_32 AC 70 ms
4,376 KB
testcase_33 AC 70 ms
4,376 KB
testcase_34 AC 69 ms
4,376 KB
testcase_35 AC 70 ms
4,376 KB
testcase_36 AC 70 ms
4,380 KB
testcase_37 AC 69 ms
4,380 KB
testcase_38 AC 70 ms
4,384 KB
testcase_39 AC 70 ms
4,376 KB
testcase_40 AC 70 ms
4,380 KB
testcase_41 AC 69 ms
4,380 KB
testcase_42 AC 70 ms
4,380 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 p[Mmax+20],id[Mmax+20];
int per[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==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,per[m]) - Mmax) % per[m], m);
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>A>>N>>M;
	
	for(x=1;x<=Mmax;x++) {
		MINUS(id);
		p[0]=1%x;
		id[1]=0;
		for(i=1;i<=Mmax;i++) {
			p[i]=p[i-1]*A%x;
			if(id[p[i]]>=0) per[x]=i-id[p[i]];
			id[p[i]]=i;
		}
	}
	
	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