結果

問題 No.181 A↑↑N mod M
ユーザー koyumeishikoyumeishi
提出日時 2015-04-06 01:52:48
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,500 bytes
コンパイル時間 687 ms
コンパイル使用メモリ 85,484 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-17 05:57:56
合計ジャッジ時間 2,245 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

long long gcd(long long a, long long b){
	if(b==0) return a;
	return gcd(b, a%b);
}


long long tot(long long n){
	long long ret = n;
	for(long long i=2; i*i<=n; i++){
		if(n%i == 0){
			ret = ret/i * (i-1);
			while(n%i == 0){
				n /= i;
			}
		}
	}
	if(n != 1){
		ret = ret/n * (n-1);
	}
	return ret;
}

long long mypow(long long x, long long y, long long MOD){
	if(x==0 && y!=0) return 0;
	long long ret=1LL;
	while(y>0LL){
		if(y&1LL) ret = (ret * x) % MOD;
		x = (x*x) % MOD;
		y >>= 1LL;
	}
	return ret;
}

vector<pair<long long, long long> > prime_factorization(long long N){
	vector< pair<long long, long long> > ret;
	
	for(long long i=2; i*i<=N; i++){
		long long tmp = 0;
		while(N%i==0){
			tmp++;
			N/=i;
		}
		if(tmp>0){
			ret.push_back( make_pair(i, tmp) );
		}
	}
	if(N!=1){
		ret.push_back( make_pair(N, 1) );
	}
	return ret;
}

int main(){
	long long A,N,M;
	cin >> A >> N >> M;

	if(N==0){
		cout << 1 << endl;
		return 0;
	}

	long long tot_ = tot(M);

	map<long long, long long> memo;

	long long ret = A;
	memo[ret] = 0;
	for(int i=1; i<N; i++){
		if( gcd(A, M) == 1 ){
			ret %= tot_;
		}
		ret = mypow(A, ret, M);

		if(memo.find(ret) != memo.end()){
			int loop = i - memo[ret];
			i = N - (N-i)%loop;
		}else{
			memo[ret] = i;
		}
	}

	cout << ret << endl;

	return 0;
}
0