結果

問題 No.336 門松列列
ユーザー ぴろずぴろず
提出日時 2016-01-15 23:52:11
言語 Java19
(openjdk 21)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 1,502 bytes
コンパイル時間 2,592 ms
コンパイル使用メモリ 73,708 KB
実行使用メモリ 68,764 KB
最終ジャッジ日時 2023-09-02 18:44:41
合計ジャッジ時間 5,363 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 162 ms
67,316 KB
testcase_01 AC 122 ms
55,456 KB
testcase_02 AC 123 ms
55,648 KB
testcase_03 AC 121 ms
55,564 KB
testcase_04 AC 170 ms
68,716 KB
testcase_05 AC 124 ms
55,784 KB
testcase_06 AC 133 ms
57,844 KB
testcase_07 AC 139 ms
60,168 KB
testcase_08 AC 150 ms
64,540 KB
testcase_09 AC 168 ms
68,288 KB
testcase_10 AC 168 ms
68,764 KB
testcase_11 AC 168 ms
68,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no336;

import java.util.Scanner;

public class Main {
	//https://oeis.org/A001250
	//https://en.wikipedia.org/wiki/Alternating_permutation
	//http://mathworld.wolfram.com/EntringerNumber.html
	
	public static long MOD = 1000000007;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		if (n == 1 || n == 2) {
			System.out.println(0);
			return;
		}
		int[][] e = new int[n+1][n+1];
		e[0][0] = 1;
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=i;j++) {
				e[i][j] = e[i][j-1] + e[i-1][i-j];
				if (e[i][j] >= MOD) {
					e[i][j] -= MOD;
				}
			}
		}
		System.out.println(e[n][n] * 2 % MOD);
	}
	
	public static int solveNaive(int n) {
		int[] p = new int[n];
		for(int i=0;i<n;i++) {
			p[i] = i+1;
		}
		int ans = 0;
		LOOP: do {
			for(int i=0;i<n-2;i++) {
				if (!isKadomatsuSequence(p[i], p[i+1], p[i+2])) {
					continue LOOP;
				}
			}
			ans++;
		}while(nextPermutation(p));
		return ans;
	}
	public static boolean isKadomatsuSequence(long a,long b,long c) {
		if (a == b || b == c) {
			return false;
		}
		return b < a && b < c || b > a && b > c;
	}
	static boolean nextPermutation(int[] p) {
		for(int a=p.length-2;a>=0;--a) {
			if(p[a]<p[a+1]) {
				for(int b=p.length-1;;--b) {
					if(p[b]>p[a]) {
						int t = p[a];
						p[a] = p[b];
						p[b] = t;
						for(++a, b=p.length-1;a<b;++a,--b) {
							t = p[a];
							p[a] = p[b];
							p[b] = t;
						}
						return true;
					}
				}
			}
		}
		return false;
	}

}
0