結果

問題 No.663 セルオートマトンの逆操作
ユーザー 37zigen37zigen
提出日時 2018-03-14 04:13:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 1,404 bytes
コンパイル時間 2,016 ms
コンパイル使用メモリ 77,808 KB
実行使用メモリ 45,796 KB
最終ジャッジ日時 2024-05-03 18:04:01
合計ジャッジ時間 6,912 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
40,296 KB
testcase_01 AC 114 ms
41,432 KB
testcase_02 AC 115 ms
41,252 KB
testcase_03 AC 114 ms
41,000 KB
testcase_04 AC 120 ms
41,128 KB
testcase_05 AC 116 ms
41,204 KB
testcase_06 AC 105 ms
39,688 KB
testcase_07 AC 117 ms
40,988 KB
testcase_08 AC 115 ms
41,024 KB
testcase_09 AC 118 ms
41,220 KB
testcase_10 AC 117 ms
41,060 KB
testcase_11 AC 119 ms
40,816 KB
testcase_12 AC 113 ms
41,088 KB
testcase_13 AC 114 ms
41,372 KB
testcase_14 AC 104 ms
39,996 KB
testcase_15 AC 122 ms
41,120 KB
testcase_16 AC 127 ms
41,000 KB
testcase_17 AC 118 ms
41,224 KB
testcase_18 AC 101 ms
39,920 KB
testcase_19 AC 105 ms
39,784 KB
testcase_20 AC 123 ms
41,496 KB
testcase_21 AC 128 ms
41,424 KB
testcase_22 AC 157 ms
42,568 KB
testcase_23 AC 182 ms
43,980 KB
testcase_24 AC 192 ms
44,424 KB
testcase_25 AC 192 ms
45,796 KB
testcase_26 AC 194 ms
45,776 KB
testcase_27 AC 115 ms
39,920 KB
testcase_28 AC 117 ms
41,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

	long MOD = 1_000_000_000 + 7;
	int[] e;
	int n;

	void run() {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		e = new int[n];
		for (int i = 0; i < e.length; ++i)
			e[i] = sc.nextInt();
		long ans = 0;
		for (int i = 0; i < 2; ++i) {
			for (int j = 0; j < 2; ++j) {
				ans = (ans + calc(i, j)) % MOD;
			}
		}
		System.out.println(ans);
	}

	long calc(int v1, int v2) {
		long[][][] dp = new long[2][2][2];
		dp[0][v1][v2] = 1;
		for (int i = 1; i < n - 1; ++i) {
			for (int tmp = 0; tmp < 2; ++tmp)
				for (int tmp2 = 0; tmp2 < 2; ++tmp2)
					dp[i % 2][tmp][tmp2] = 0;
			for (int u = 0; u <= 1; ++u) {
				for (int v = 0; v <= 1; ++v) {
					for (int w = 0; w <= 1; ++w) {
						if (f(u, v, w) == e[i]) {
							dp[i % 2][v][w] = (dp[i % 2][v][w] + dp[(i % 2) ^ 1][u][v]) % MOD;
						}
					}
				}
			}
		}
		long ret = 0;
		for (int i = 0; i <= 1; ++i) {
			for (int j = 0; j <= 1; ++j) {
				if (f(i, j, v1) == e[n - 1] && f(j, v1, v2) == e[0]) {
					ret = (ret + dp[(n % 2)][i][j]) % MOD;
				}
			}
		}
		return ret;
	}

	int f(int a, int b, int c) {
		int[] ret = new int[] { 0, 1, 1, 1, 0, 1, 1, 0 };
		return ret[a << 2 | b << 1 | c];
	}

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