結果

問題 No.181 A↑↑N mod M
ユーザー testtest
提出日時 2015-11-24 21:40:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,424 bytes
コンパイル時間 156 ms
コンパイル使用メモリ 26,584 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-15 19:02:15
合計ジャッジ時間 1,637 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 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,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 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,380 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,500 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,380 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 3 ms
4,380 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

int const MAX = 2010; 

int _pow(int A, int N, int M){
	if(0 >= N){ return 1; } 
	if(0 >= A){ return 0; } 
	if(1 >= M){ return 0; } 
	int b = _pow(A, N / 2, M); b = (b * b) % M; 
	if(0 == N % 2){ return b; }
	return (b * A) % M; 
}

void GetK0D(int A, int M, int &k0, int &d){
	static int memo[MAX]; 
	int r = 1, i = 0, j = 0; 
	for(i = 1; i <= M; ++i){
		memo[i-1] = r; r *= A; r %= M; 
		for(j = 0; j < i; ++j){
			if(r == memo[j]){ k0 = j; d = i - j; return; }
		}
	}
	k0 = -1; d = -1; 	
}

bool Flag(int A, int N, int k0){
	if(1 == N){ return k0 < A; } 
	if(5 <= A){ return true; } 
	if(4 == A){
		if(2 == N){ return k0 < 4 * 4 * 4 * 4; } 
		return true;  
	}else if(3 == A){
		if(2 == N){ return k0 < 3 * 3 * 3; } 
		return true; 
	}else{
		if(2 == N){ return k0 < 2 * 2; } 
		if(3 == N){ return k0 < 2 * 2 * 2 * 2; }
		return true; 
	}
	return false; 
}

int Arrow(int const A, int N, int const M){
	int B = A % M; 
	if(1 >= M){ return 0; } 
	if(0 >= N){ return 1; }
	if(1 == N){ return B; } 
	if(0 >= B){ return 0; }
	if(1 == B){ return 1; } 
	int k0, d; GetK0D(B, M, k0, d);
	if(-1 == k0){ return 0; }
	if(Flag(A, N - 1, k0)){ 
		int b = Arrow(A, N - 1, d); b = ((b - k0) % d + d) % d;
		return _pow(B, k0 + b, M); 
	}
	return _pow(B, Arrow(A, N - 1, M), M); 
}

int main(int argc, char *argv[]){
	int A, N, M; 

	scanf("%d %d %d", &A, &N, &M); 
	printf("%d\n", Arrow(A, N, M)); 

	return 0;
}

0