結果

問題 No.336 門松列列
ユーザー 37zigen37zigen
提出日時 2016-06-21 00:13:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 996 bytes
コンパイル時間 2,081 ms
コンパイル使用メモリ 73,828 KB
実行使用メモリ 56,100 KB
最終ジャッジ日時 2023-09-02 18:45:16
合計ジャッジ時間 4,761 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 155 ms
56,100 KB
testcase_01 AC 121 ms
55,604 KB
testcase_02 AC 122 ms
55,696 KB
testcase_03 AC 123 ms
56,056 KB
testcase_04 AC 158 ms
55,584 KB
testcase_05 AC 122 ms
55,388 KB
testcase_06 AC 144 ms
55,828 KB
testcase_07 AC 150 ms
55,812 KB
testcase_08 AC 150 ms
55,948 KB
testcase_09 AC 158 ms
55,888 KB
testcase_10 AC 158 ms
53,928 KB
testcase_11 AC 157 ms
55,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.Scanner;

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

	final long MOD = 1000000007;

	void solver() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		if (N == 1 || N == 2) {
			System.out.println(0);
			return;
		}
		long[] c = new long[N + 2];
		c[0] = 1;
		c[1] = 2;
		c[2] = 1;
		long inv_4=250000002;
		
		long[] dp = new long[N + 1];// dp[i]:N=iの時の門松列の数
		dp[0] = 2;
		dp[1] = 2;
		dp[2] = 2;
		for (int i = 2; i < N; i++) {
			for (int j = 0; j <= i; j++) {
				dp[i + 1] += dp[j] * dp[i - j]  % MOD * c[j] % MOD*inv_4;
				dp[i + 1] %= MOD;
				if (dp[i + 1] < 0 || c[j] <= 0) {
					throw new AssertionError("error");
				}
			}
			for (int j = i + 1; j >= 0; j--) {
				if (j != 0) {
					c[j] = c[j] + c[j - 1];
				} else {
					c[j] = 1;
				}
				c[j] %= MOD;
				if (c[j] < 0)
					throw new AssertionError();
			}
		}
		System.out.println(dp[N]);
		sc.close();
	}
}
0