結果

問題 No.93 ペガサス
ユーザー uwiuwi
提出日時 2014-12-10 14:12:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 355 ms / 5,000 ms
コード長 6,039 bytes
コンパイル時間 2,522 ms
コンパイル使用メモリ 90,568 KB
実行使用メモリ 55,764 KB
最終ジャッジ日時 2023-09-02 13:55:40
合計ジャッジ時間 7,025 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
50,628 KB
testcase_01 AC 65 ms
50,576 KB
testcase_02 AC 66 ms
50,712 KB
testcase_03 AC 65 ms
50,404 KB
testcase_04 AC 135 ms
54,292 KB
testcase_05 AC 106 ms
53,824 KB
testcase_06 AC 115 ms
53,876 KB
testcase_07 AC 297 ms
55,076 KB
testcase_08 AC 171 ms
54,344 KB
testcase_09 AC 323 ms
54,896 KB
testcase_10 AC 72 ms
51,212 KB
testcase_11 AC 79 ms
50,888 KB
testcase_12 AC 332 ms
55,276 KB
testcase_13 AC 192 ms
53,856 KB
testcase_14 AC 111 ms
53,836 KB
testcase_15 AC 313 ms
55,452 KB
testcase_16 AC 118 ms
53,792 KB
testcase_17 AC 70 ms
50,476 KB
testcase_18 AC 69 ms
50,976 KB
testcase_19 AC 355 ms
55,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";
	
	// 2 2 8 28 152 952 7208
	static void solve()
	{
		out.println(go(ni()));
//		for(int i = 3;i <= 9;i++){
//			tr(i, go(i));
//		}
	}
	
	static long go(int n){
		if(n == 1)return 1;
		int mod = 1000000007;
//		int n = ni();
		long[][][] pre = new long[n+1][2][2]; // [violateしている列の個数][2個上の行がviolateしているかどうか][1個上の行がviolateしているかどうか]
		long[][][] cur = new long[n+1][2][2];
		pre[0][0][0] = 2;
		for(int i = 2;i < n;i++){
			for(int j = 0;j <= n;j++){
				for(int k = 0;k < 2;k++){
					for(int l = 0;l < 2;l++){
						cur[j][k][l] = 0;
					}
				}
			}
			
			for(int j = 0;j <= i;j++){
				for(int k = 0;k < 2;k++){
					// 3個上以上:切断しない, 2-4:切断しない, 1-3:切断しない, 0-2:なにもない
					if(i+1-j-(2-k) >= 0){
						cur[j][0][0] += pre[j][k][0] * (i+1-j-(2-k));
						cur[j][0][0] %= mod;
					}
					if(i+1-j-(2-k) >= 0){
						cur[j][1][0] += pre[j][k][1] * (i+1-j-(2-k));
						cur[j][1][0] %= mod;
					}
					
					// 3個上以上:切断する, 2-4:切断しない, 1-3:切断しない, 0-2:なにもない
					if(j-1 >= 0){
						cur[j-1][0][0] += pre[j][k][0] * (j-k-0);
						cur[j-1][0][0] %= mod;
						cur[j-1][1][0] += pre[j][k][1] * (j-k-1);
						cur[j-1][1][0] %= mod;
					}
					
					// 3個上以上:切断しない, 2-4:切断する, 1-3:切断しない, 0-2:結合する
					if(k == 1 && j >= 1){
						cur[j][0][1] += pre[j][k][0];
						if(cur[j][0][1] >= mod)cur[j][0][1] -= mod;
						cur[j][1][1] += pre[j][k][1];
						if(cur[j][1][1] >= mod)cur[j][1][1] -= mod;
					}
					
					// 3個上以上:切断しない, 2-4:切断しない, 1-3:切断しない, 0-2:結合する
					if(j+1 <= n){
						cur[j+1][0][1] += pre[j][k][0] * (2-k);
						cur[j+1][0][1] %= mod;
						cur[j+1][1][1] += pre[j][k][1] * (2-k);
						cur[j+1][1][1] %= mod;
					}
					
					// 3個上以上:切断しない, 2-4:切断しない, 1-3:切断する, 0-2:なにもない
					if(j-1 >= 0){
						cur[j-1][0][0] += pre[j][k][1];
						cur[j-1][0][0] %= mod;
					}
				}
			}
			long[][][] dum = pre; pre = cur; cur = dum;
		}
		return pre[0][0][0];
	}
	
	static boolean nextPermutation(int[] src) {
		int i;
		for (i = src.length - 2; i >= 0 && src[i] > src[i + 1]; i--)
			;
		if (i == -1)
			return false;
		int j;
		for (j = i + 1; j < src.length && src[i] < src[j]; j++)
			;
		int d = src[i];
		src[i] = src[j - 1];
		src[j - 1] = d;
		for (int p = i + 1, q = src.length - 1; p < q; p++, q--) {
			d = src[p];
			src[p] = src[q];
			src[q] = d;
		}
		return true;
	}
	
	public static void main(String[] args) throws Exception
	{
		long S = System.currentTimeMillis();
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
		long G = System.currentTimeMillis();
		tr(G-S+"ms");
	}
	
	private static boolean eof()
	{
		if(lenbuf == -1)return true;
		int lptr = ptrbuf;
		while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
		
		try {
			is.mark(1000);
			while(true){
				int b = is.read();
				if(b == -1){
					is.reset();
					return true;
				}else if(!isSpaceChar(b)){
					is.reset();
					return false;
				}
			}
		} catch (IOException e) {
			return true;
		}
	}
	
	private static byte[] inbuf = new byte[1024];
	static int lenbuf = 0, ptrbuf = 0;
	
	private static int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private static double nd() { return Double.parseDouble(ns()); }
	private static char nc() { return (char)skip(); }
	
	private static String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private static char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private static char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private static int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private static int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
0