結果

問題 No.181 A↑↑N mod M
ユーザー ぴろずぴろず
提出日時 2015-04-03 21:18:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 131 ms / 5,000 ms
コード長 2,204 bytes
コンパイル時間 4,044 ms
コンパイル使用メモリ 81,612 KB
実行使用メモリ 56,292 KB
最終ジャッジ日時 2023-08-15 18:54:25
合計ジャッジ時間 9,481 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,952 KB
testcase_01 AC 125 ms
55,656 KB
testcase_02 AC 125 ms
55,628 KB
testcase_03 AC 124 ms
55,524 KB
testcase_04 AC 124 ms
55,876 KB
testcase_05 AC 126 ms
55,784 KB
testcase_06 AC 125 ms
55,696 KB
testcase_07 AC 126 ms
55,852 KB
testcase_08 AC 126 ms
55,740 KB
testcase_09 AC 124 ms
55,520 KB
testcase_10 AC 124 ms
55,620 KB
testcase_11 AC 124 ms
55,900 KB
testcase_12 AC 124 ms
55,832 KB
testcase_13 AC 123 ms
55,540 KB
testcase_14 AC 123 ms
55,240 KB
testcase_15 AC 127 ms
55,972 KB
testcase_16 AC 131 ms
55,636 KB
testcase_17 AC 124 ms
55,640 KB
testcase_18 AC 123 ms
55,476 KB
testcase_19 AC 124 ms
55,936 KB
testcase_20 AC 123 ms
55,600 KB
testcase_21 AC 125 ms
55,836 KB
testcase_22 AC 124 ms
55,624 KB
testcase_23 AC 121 ms
55,900 KB
testcase_24 AC 121 ms
55,608 KB
testcase_25 AC 121 ms
55,628 KB
testcase_26 AC 120 ms
55,636 KB
testcase_27 AC 121 ms
55,588 KB
testcase_28 AC 120 ms
55,672 KB
testcase_29 AC 122 ms
55,500 KB
testcase_30 AC 126 ms
55,496 KB
testcase_31 AC 121 ms
55,468 KB
testcase_32 AC 123 ms
55,436 KB
testcase_33 AC 121 ms
55,832 KB
testcase_34 AC 123 ms
55,748 KB
testcase_35 AC 121 ms
56,292 KB
testcase_36 AC 122 ms
56,072 KB
testcase_37 AC 122 ms
55,624 KB
testcase_38 AC 122 ms
55,632 KB
testcase_39 AC 122 ms
55,612 KB
testcase_40 AC 122 ms
55,736 KB
testcase_41 AC 121 ms
54,176 KB
testcase_42 AC 122 ms
55,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package tetration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(solve(sc.nextInt(),sc.nextInt(),sc.nextInt()));
	}

	static final int M_MAX = 2010;
	static int bmax = 0;
	static ArrayList<Long> hyper4Small;
	static int[] memo = new int[M_MAX]; //周期の計算用

	public static long solve(long a,long b,long m) {
		if (a == 1) {
			return 1 % m;
		}

		/*
		 * 小さいbに対して、hyper4(a,b)を愚直に計算する。
		 * hyper4(a,b) < M_MAXとなる最大のb,bmaxを求める。
		 * a = 1の場合そのようなbは存在しないのであらかじめ弾く。
		 */
		hyper4Small = new ArrayList<>();
		hyper4Small.add(1L);
		LOOP: for(int i=1;;i++) {
			long x = 1;
			long exponent = hyper4Small.get(i-1);
			for(int j=0;j<exponent;j++) {
				x *= a;
				if (x >= M_MAX) {
					break LOOP;
				}
			}
			hyper4Small.add(x);
		}
		bmax = hyper4Small.size() - 1;

		return hyper4Mod(a, b, m);
	}

	public static long hyper4Mod(long a,long b,long m) {
		//自明なケース
		if (m == 1) {
			return 0;
		}
		if (b == 0) {
			return 1;
		}

		if (b - 1 <= bmax) { //hyper4(a,b-1) <= M_MAX と同じ
			//前計算を用いる
			return powMod(a, hyper4Small.get((int)b-1) , m);
		}else{
			//powmodの周期性を用いる
			long c = cycle(a,m);
			return powMod(a, M_MAX + mod((hyper4Mod(a, b-1, c) - M_MAX), c), m);
		}
	}

	/**
	 *  1,a,a^2,a^3,... (mod m) がループする周期を求める。
	 *  例えばa=2,m=28のとき、
	 *  1,2,4,8,16,4,8,16,...
	 *  で返り値は3となる。
	 */
	public static long cycle(long a,long m) {
		Arrays.fill(memo, -1);
		long x = powMod(a,M_MAX,m);
		for(int i=0;;i++) {
			int j = memo[(int) x];
			if (j >= 0) {
				return i - j;
			}
			memo[(int) x] = i;
			x = x * a % m;
		}
	}


	public static long powMod(long x,long n,long mod) {
		long res = 1;
		while(n > 0) {
			if ((n & 1) > 0) {
				res = (res * x) % mod;
			}
			x = (x * x) % mod;
			n/=2;
		}
		return res;
	}

	public static long mod(long x,long m) {
		x %= m;
		if (x < 0) {
			x += m;
		}
		return x;
	}

}
0