結果

問題 No.1073 無限すごろく
ユーザー ks2mks2m
提出日時 2020-06-05 23:03:26
言語 Java21
(openjdk 21)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,620 bytes
コンパイル時間 2,137 ms
コンパイル使用メモリ 74,316 KB
実行使用メモリ 56,280 KB
最終ジャッジ日時 2023-08-22 17:09:01
合計ジャッジ時間 7,972 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,636 KB
testcase_01 AC 126 ms
55,752 KB
testcase_02 AC 126 ms
55,892 KB
testcase_03 AC 128 ms
55,828 KB
testcase_04 AC 130 ms
55,680 KB
testcase_05 AC 127 ms
55,832 KB
testcase_06 AC 128 ms
55,936 KB
testcase_07 AC 127 ms
55,716 KB
testcase_08 AC 127 ms
55,480 KB
testcase_09 AC 128 ms
55,712 KB
testcase_10 AC 126 ms
55,832 KB
testcase_11 AC 126 ms
55,852 KB
testcase_12 AC 128 ms
55,932 KB
testcase_13 AC 128 ms
55,960 KB
testcase_14 AC 128 ms
55,836 KB
testcase_15 AC 126 ms
55,692 KB
testcase_16 AC 126 ms
55,696 KB
testcase_17 AC 127 ms
55,852 KB
testcase_18 AC 129 ms
55,928 KB
testcase_19 AC 126 ms
55,828 KB
testcase_20 AC 127 ms
55,896 KB
testcase_21 AC 126 ms
55,960 KB
testcase_22 AC 126 ms
56,280 KB
testcase_23 AC 125 ms
55,760 KB
testcase_24 AC 127 ms
55,448 KB
testcase_25 AC 129 ms
55,480 KB
testcase_26 AC 125 ms
55,824 KB
testcase_27 AC 126 ms
55,964 KB
testcase_28 AC 123 ms
55,840 KB
testcase_29 AC 124 ms
55,620 KB
testcase_30 AC 125 ms
55,920 KB
testcase_31 AC 125 ms
55,616 KB
testcase_32 AC 126 ms
55,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		sc.close();

		int mod = 1000000007;
		int k = 6;
		long[][][] c = new long[61][k][k];
		for (int i = 0, j = 1; i < k; i++, j *= 6) {
			c[0][0][i] = j;
		}
		for (int i = 1; i < k; i++) {
			c[0][i][i - 1] = 1;
		}
		for (int i = 0; i < c.length - 1; i++) {
			for (int j = 0; j < k; j++) {
				for (int j2 = 0; j2 < k; j2++) {
					for (int x = 0; x < k; x++) {
						c[i + 1][j][j2] += c[i][j][x] * c[i][x][j2];
						c[i + 1][j][j2] %= mod;
					}
				}
			}
		}

		long ans = 0;
		long[] a = {2401, 343, 49, 7, 1, 1};
		if (n < 6) {
			ans = a[(int) (5 - n)];
		} else {
			for (int i = 0; i < c.length; i++) {
				if ((n >> i & 1) == 1) {
					long[] tmp = new long[k];
					for (int j = 0; j < k; j++) {
						for (int x = 0; x < k; x++) {
							tmp[j] += c[i][j][x] * a[x];
							tmp[j] %= mod;
						}
					}
					a = tmp;
				}
			}
			ans = a[5];
		}

		long div = power(6, n, mod);
		ans = ans * modinv(div, mod) % mod;
		System.out.println(ans);
	}

	static long power(long x, long n, int m) {
		if (n == 0) {
			return 1;
		}
		long val = power(x, n / 2, m);
		val = val * val % m;
		if (n % 2 == 1) {
			val = val * x % m;
		}
		return val;
	}

	static long modinv(long a, int m) {
		long b = m;
		long u = 1;
		long v = 0;
		long tmp = 0;

		while (b > 0) {
			long t = a / b;
			a -= t * b;
			tmp = a;
			a = b;
			b = tmp;

			u -= t * v;
			tmp = u;
			u = v;
			v = tmp;
		}

		u %= m;
		if (u < 0) u += m;
		return u;
	}
}
0