結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー 37zigen37zigen
提出日時 2017-12-25 03:49:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 170 ms / 3,000 ms
コード長 2,481 bytes
コンパイル時間 3,543 ms
コンパイル使用メモリ 74,660 KB
実行使用メモリ 57,552 KB
最終ジャッジ日時 2023-08-22 18:15:57
合計ジャッジ時間 15,625 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
55,792 KB
testcase_01 AC 156 ms
55,940 KB
testcase_02 AC 131 ms
55,888 KB
testcase_03 AC 134 ms
55,864 KB
testcase_04 AC 135 ms
55,740 KB
testcase_05 AC 137 ms
55,496 KB
testcase_06 AC 156 ms
55,948 KB
testcase_07 AC 153 ms
55,888 KB
testcase_08 AC 158 ms
56,076 KB
testcase_09 AC 154 ms
55,928 KB
testcase_10 AC 156 ms
55,852 KB
testcase_11 AC 153 ms
53,804 KB
testcase_12 AC 156 ms
56,204 KB
testcase_13 AC 158 ms
55,876 KB
testcase_14 AC 162 ms
55,856 KB
testcase_15 AC 160 ms
56,192 KB
testcase_16 AC 160 ms
56,188 KB
testcase_17 AC 159 ms
55,984 KB
testcase_18 AC 159 ms
55,644 KB
testcase_19 AC 161 ms
56,216 KB
testcase_20 AC 162 ms
55,656 KB
testcase_21 AC 164 ms
56,184 KB
testcase_22 AC 161 ms
56,180 KB
testcase_23 AC 162 ms
55,628 KB
testcase_24 AC 164 ms
55,972 KB
testcase_25 AC 166 ms
55,948 KB
testcase_26 AC 162 ms
55,980 KB
testcase_27 AC 164 ms
56,188 KB
testcase_28 AC 166 ms
56,004 KB
testcase_29 AC 166 ms
56,204 KB
testcase_30 AC 166 ms
55,932 KB
testcase_31 AC 167 ms
56,104 KB
testcase_32 AC 164 ms
55,820 KB
testcase_33 AC 166 ms
55,976 KB
testcase_34 AC 165 ms
55,796 KB
testcase_35 AC 164 ms
55,816 KB
testcase_36 AC 165 ms
55,900 KB
testcase_37 AC 167 ms
55,968 KB
testcase_38 AC 166 ms
55,948 KB
testcase_39 AC 167 ms
55,956 KB
testcase_40 AC 167 ms
56,060 KB
testcase_41 AC 166 ms
55,812 KB
testcase_42 AC 167 ms
55,632 KB
testcase_43 AC 166 ms
55,872 KB
testcase_44 AC 166 ms
55,580 KB
testcase_45 AC 167 ms
55,844 KB
testcase_46 AC 167 ms
56,504 KB
testcase_47 AC 170 ms
56,072 KB
testcase_48 AC 166 ms
56,188 KB
testcase_49 AC 166 ms
57,552 KB
testcase_50 AC 167 ms
55,948 KB
testcase_51 AC 166 ms
53,784 KB
testcase_52 AC 165 ms
55,852 KB
testcase_53 AC 166 ms
56,268 KB
testcase_54 AC 165 ms
55,776 KB
testcase_55 AC 167 ms
56,168 KB
testcase_56 AC 168 ms
56,120 KB
testcase_57 AC 168 ms
56,012 KB
testcase_58 AC 163 ms
55,580 KB
testcase_59 AC 168 ms
55,784 KB
testcase_60 AC 165 ms
55,636 KB
testcase_61 AC 168 ms
55,956 KB
testcase_62 AC 170 ms
55,860 KB
testcase_63 AC 162 ms
55,976 KB
testcase_64 AC 163 ms
55,816 KB
testcase_65 AC 161 ms
55,928 KB
testcase_66 AC 161 ms
55,988 KB
testcase_67 AC 163 ms
55,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main {

	int pow(int a, int n) {
		return (int) Math.pow(a, n);
	}

	int at(int a, int pos) {
		return a / pow(3, pos) % 3;
	}

	final long MOD = 1_000_000_000 + 7;

	long[][] mul(long[][] a, long[][] b) {
		long[][] ret = new long[a.length][b[0].length];
		for (int i = 0; i < a.length; ++i) {
			for (int j = 0; j < b[i].length; ++j) {
				for (int k = 0; k < a[i].length; ++k) {
					ret[i][j] += a[i][k] * b[k][j] % MOD;
					ret[i][j] %= MOD;
				}
			}
		}
		return ret;
	}

	long[][] pow(long[][] a, long n) {
		long[][] ret = new long[a.length][a.length];
		for (int i = 0; i < ret.length; ++i)
			ret[i][i] = 1;
		for (; n > 0; n >>= 1, a = mul(a, a)) {
			if (n % 2 == 1) {
				ret = mul(ret, a);
			}
		}
		return ret;
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long[][] mat = new long[3 * 3 * 3][3 * 3 * 3];
		//0:空白
		//1:未完成
		//2:完成
		for (int from = 0; from < 3 * 3 * 3; ++from) {
			in: for (int to = 0; to < 3 * 3 * 3; ++to) {
				int cur = to;
				{//未完成が前列に残っていない
					for (int i = 0; i < 3; ++i) {
						if (at(from, i) == 1 && at(to, i) != 2) {
							continue in;
						} else if (at(from, i) == 1 && at(to, i) == 2) {
							cur = cur - pow(3, i);
						}
					}
				}
				if (at(cur, 0) == 2 && at(cur, 1) == 2) {
					cur -= pow(3, 0);
					cur -= pow(3, 1);
				}
				if (at(cur, 1) == 2 && at(cur, 2) == 2) {
					cur -= pow(3, 1);
					cur -= pow(3, 2);
				}
				if (at(cur, 0) == 2 || at(cur, 1) == 2 || at(cur, 2) == 2) {
					continue in;
				}
				{//空白が無い
					for (int i = 0; i < 3; ++i) {
						if (at(from, i) == 0 && at(to, i) == 0)
							continue in;
					}
					if (at(to, 0) == 0 && at(to, 1) == 0)
						continue in;
					if (at(to, 1) == 0 && at(to, 2) == 0)
						continue in;
					if (at(from, 0) == 0 && at(from, 1) == 0)
						continue in;
					if (at(from, 1) == 0 && at(from, 2) == 0)
						continue in;

				}
				mat[to][from]++;
			}
		}
		long ans = 0;
		long[][] v = new long[3 * 3 * 3][1];
		v[3 * 3 * 3 - 1][0] = 1;
		v = mul(pow(mat, n), v);
		for (int i = 0; i < 3 * 3 * 3; ++i) {
			if (at(i, 0) == 1 || at(i, 1) == 1 || at(i, 2) == 1)
				continue;
			ans = (ans + v[i][0]) % MOD;
		}
		System.out.println(ans);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

	public static void main(String[] args) {
		new Main().run();
	}
}
0