結果

問題 No.663 セルオートマトンの逆操作
ユーザー 37zigen37zigen
提出日時 2018-03-14 04:13:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 1,404 bytes
コンパイル時間 3,680 ms
コンパイル使用メモリ 74,332 KB
実行使用メモリ 59,124 KB
最終ジャッジ日時 2023-08-16 09:14:34
合計ジャッジ時間 8,906 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,852 KB
testcase_01 AC 124 ms
55,756 KB
testcase_02 AC 125 ms
55,928 KB
testcase_03 AC 122 ms
55,768 KB
testcase_04 AC 122 ms
55,740 KB
testcase_05 AC 121 ms
55,776 KB
testcase_06 AC 128 ms
55,600 KB
testcase_07 AC 124 ms
53,920 KB
testcase_08 AC 127 ms
55,776 KB
testcase_09 AC 126 ms
56,020 KB
testcase_10 AC 127 ms
55,512 KB
testcase_11 AC 123 ms
55,552 KB
testcase_12 AC 125 ms
55,508 KB
testcase_13 AC 125 ms
55,912 KB
testcase_14 AC 125 ms
55,864 KB
testcase_15 AC 127 ms
55,512 KB
testcase_16 AC 128 ms
55,936 KB
testcase_17 AC 124 ms
55,984 KB
testcase_18 AC 125 ms
56,152 KB
testcase_19 AC 126 ms
55,720 KB
testcase_20 AC 135 ms
56,124 KB
testcase_21 AC 141 ms
56,096 KB
testcase_22 AC 183 ms
58,264 KB
testcase_23 AC 194 ms
56,552 KB
testcase_24 AC 191 ms
59,124 KB
testcase_25 AC 205 ms
58,700 KB
testcase_26 AC 206 ms
58,948 KB
testcase_27 AC 124 ms
55,468 KB
testcase_28 AC 128 ms
55,692 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