結果

問題 No.936 Are
ユーザー uwiuwi
提出日時 2019-11-30 17:11:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,289 ms / 3,000 ms
コード長 9,238 bytes
コンパイル時間 4,537 ms
コンパイル使用メモリ 89,572 KB
実行使用メモリ 59,384 KB
最終ジャッジ日時 2024-11-21 01:28:20
合計ジャッジ時間 19,220 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
43,864 KB
testcase_01 AC 339 ms
51,228 KB
testcase_02 AC 1,289 ms
59,384 KB
testcase_03 AC 85 ms
47,176 KB
testcase_04 AC 104 ms
47,176 KB
testcase_05 AC 79 ms
44,768 KB
testcase_06 AC 86 ms
47,336 KB
testcase_07 AC 66 ms
44,488 KB
testcase_08 AC 59 ms
44,316 KB
testcase_09 AC 85 ms
47,388 KB
testcase_10 AC 83 ms
45,300 KB
testcase_11 AC 87 ms
47,336 KB
testcase_12 AC 61 ms
43,864 KB
testcase_13 AC 491 ms
52,780 KB
testcase_14 AC 1,040 ms
53,040 KB
testcase_15 AC 530 ms
52,680 KB
testcase_16 AC 845 ms
52,756 KB
testcase_17 AC 735 ms
52,844 KB
testcase_18 AC 458 ms
53,040 KB
testcase_19 AC 1,198 ms
58,092 KB
testcase_20 AC 1,257 ms
58,644 KB
testcase_21 AC 1,161 ms
52,380 KB
testcase_22 AC 734 ms
58,348 KB
testcase_23 AC 1,250 ms
51,884 KB
testcase_24 AC 1,150 ms
52,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest191129;
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 E2 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	boolean win(int j, int k, int l, int m)
	{
		if(j + k == 0)return false;
		if(j > 0 && l > 0 && m == 0 && j+l == 5)return true;
		if(j > 0 && m > 0 && l == 0 && j+m == 5)return true;
		if(k > 0 && l > 0 && m == 0 && k+l == 5)return true;
		if(k > 0 && m > 0 && l == 0 && k+m == 5)return true;
		return false;
	}
	
	void solve()
	{
		int n = ni(), K = ni();
		long[][][][] dp = new long[5][5][5][5];
		int[] a = na(4);
		
		boolean[][][][] win = new boolean[5][5][5][5];
		for(int i = 0;i < 5;i++) {
			for(int j = 0;j < 5;j++) {
				for(int k = 0;k < 5;k++) {
					for(int l = 0;l < 5;l++) {
						win[i][j][k][l] = win(i, j, k, l);
					}
				}
			}
		}
		
		int[][][][][][] nexsh = new int[5][5][5][5][][];
		int[][][][][][] nexsi = new int[5][5][5][5][][];
		
		for(int j = 0;j < 5;j++) {
			for(int k = 0;k < 5;k++) {
				for(int l = 0;l < 5;l++) {
					for(int m = 0;m < 5;m++) {
						if(j + k == 0)continue;
						if(l + m == 0)continue;
						if(win[j][k][l][m])continue; // 必勝ならおわり
						nexsh[j][k][l][m] = new int[15][];
						int p = 0;
						boolean allose = true;
						if(j > 0 && l > 0){
							int nl = (j+l)%5;
							if(!win[nl][m][j][k]) {
								allose = false;
								nexsh[j][k][l][m][p++] = new int[] {nl, m, j, k};
							}
						}
						if(j > 0 && m > 0){
							int nm = (j+m)%5;
							if(!win[l][nm][j][k]) {
								allose = false;
								nexsh[j][k][l][m][p++] = new int[] {l, nm, j, k};
							}
						}
						if(k > 0 && l > 0){
							int nl = (k+l)%5;
							if(!win[nl][m][j][k]) {
								allose = false;
								nexsh[j][k][l][m][p++] = new int[] {nl, m, j, k};
							}
						}
						if(k > 0 && m > 0){
							int nm = (k+m)%5;
							if(!win[l][nm][j][k]) {
								allose = false;
								nexsh[j][k][l][m][p++] = new int[] {l, nm, j, k};
							}
						}
						{
							int s = j+k;
							for(int t = 0;t <= s;t++) {
								if(t == j || t == k)continue;
								if(t >= 5 || s-t >= 5)continue;
								if(!win(l, m, t, s-t)) {
									allose = false;
									nexsh[j][k][l][m][p++] = new int[] {l, m, t, s-t};
								}
							}
							s -= 5;
							if(s > 0) { // 自滅はしない
								for(int t = 0;t <= s;t++) {
									if(!win(l, m, t, s-t)) {
										allose = false;
										nexsh[j][k][l][m][p++] = new int[] {l, m, t, s-t};
									}
								}
							}
						}
						
						if(allose) {
							// 必敗なら取れる手すべてを適当にとる
//							tr(j, k, l, m, dp[j][k][l][m]);
							if(j > 0 && l > 0){
								int nl = (j+l)%5;
								nexsh[j][k][l][m][p++] = new int[] {nl, m, j, k};
							}
							if(j > 0 && m > 0){
								int nm = (j+m)%5;
								nexsh[j][k][l][m][p++] = new int[] {l, nm, j, k};
							}
							if(k > 0 && l > 0){
								int nl = (k+l)%5;
								nexsh[j][k][l][m][p++] = new int[] {nl, m, j, k};
							}
							if(k > 0 && m > 0){
								int nm = (k+m)%5;
								nexsh[j][k][l][m][p++] = new int[] {l, nm, j, k};
							}
							{
								int s = j+k;
								for(int t = 0;t <= s;t++) {
									if(t == j || t == k)continue;
									if(t >= 5 || s-t >= 5)continue;
									nexsh[j][k][l][m][p++] = new int[] {l, m, t, s-t};
								}
								s -= 5;
								if(s > 0) {
									for(int t = 0;t <= s;t++) {
										nexsh[j][k][l][m][p++] = new int[] {l, m, t, s-t};
									}
								}
							}
						}
						nexsh[j][k][l][m] = Arrays.copyOf(nexsh[j][k][l][m], p);
//						tr(j, k, l, m, nexsh[j][k][l][m]);
					}
				}
			}
		}
		
		for(int j = 0;j < 5;j++) {
			for(int k = 0;k < 5;k++) {
				for(int l = 0;l < 5;l++) {
					for(int m = 0;m < 5;m++) {
						if(j + k == 0)continue;
						if(l + m == 0)continue;
						nexsi[j][k][l][m] = new int[15][];
						int p = 0;

						if(j > 0 && l > 0){
							int nl = (j+l)%5;
							nexsi[j][k][l][m][p++] = new int[] {nl, m, j, k};
						}
						if(j > 0 && m > 0){
							int nm = (j+m)%5;
							nexsi[j][k][l][m][p++] = new int[] {l, nm, j, k};
						}
						if(k > 0 && l > 0){
							int nl = (k+l)%5;
							nexsi[j][k][l][m][p++] = new int[] {nl, m, j, k};
						}
						if(k > 0 && m > 0){
							int nm = (k+m)%5;
							nexsi[j][k][l][m][p++] = new int[] {l, nm, j, k};
						}
						{
							int s = j+k;
							for(int t = 0;t <= s;t++) {
								if(t == j || t == k)continue;
								if(t >= 5 || s-t >= 5)continue;
								nexsi[j][k][l][m][p++] = new int[] {l, m, t, s-t};
							}
							s -= 5;
							if(s > 0) {
								for(int t = 0;t <= s;t++) {
									nexsi[j][k][l][m][p++] = new int[] {l, m, t, s-t};
								}
							}
						}
						nexsi[j][k][l][m] = Arrays.copyOf(nexsi[j][k][l][m], p);
//						if(j == 0)tr(j, k, l, m, nexsi[j][k][l][m]);
					}
				}
			}
		}
		
		if(n == 0) {
			dp[a[0]][a[1]][a[2]][a[3]] = 1;
			if(K % 2 == 0)K--;
		}else {
			dp[a[2]][a[3]][a[0]][a[1]] = 1;
			if(K % 2 == 1)K--;
		}
		
		long ans = 0;
		int mod = 1000000007;
		for(int i = 0;i <= K;i++) {
			for(int j = 0;j < 5;j++) {
				for(int k = 0;k < 5;k++) {
					if(((n^i)&1) == 1){
						ans += dp[0][0][j][k];
					}
					dp[0][0][j][k] = 0;
				}
			}
			if(i == K)break;
			
			long[][][][] ndp = new long[5][5][5][5];
			if(((n^i)&1) == 1){
				// hasitaka
				for(int j = 0;j < 5;j++) {
					for(int k = 0;k < 5;k++) {
						for(int l = 0;l < 5;l++) {
							for(int m = 0;m < 5;m++) {
								if(dp[j][k][l][m] == 0)continue;
								if(nexsh[j][k][l][m] == null)continue;
								for(int[] ne : nexsh[j][k][l][m]) {
									ndp[ne[0]][ne[1]][ne[2]][ne[3]] += dp[j][k][l][m];
									if(ndp[ne[0]][ne[1]][ne[2]][ne[3]] >= mod)ndp[ne[0]][ne[1]][ne[2]][ne[3]] -= mod;
								}
							}
						}
					}
				}
			}else {
				// iot
				for(int j = 0;j < 5;j++) {
					for(int k = 0;k < 5;k++) {
						for(int l = 0;l < 5;l++) {
							for(int m = 0;m < 5;m++) {
								if(dp[j][k][l][m] == 0)continue;
								if(nexsi[j][k][l][m] == null)continue;
								for(int[] ne : nexsi[j][k][l][m]) {
									ndp[ne[0]][ne[1]][ne[2]][ne[3]] += dp[j][k][l][m];
									if(ndp[ne[0]][ne[1]][ne[2]][ne[3]] >= mod)ndp[ne[0]][ne[1]][ne[2]][ne[3]] -= mod;
								}
							}
						}
					}
				}
			}
			dp = ndp;
			
		}
		out.println(ans%mod);
	}
	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new E2().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private 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 boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private 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 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 int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private 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 int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private 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) { System.out.println(Arrays.deepToString(o)); }
}
0