結果

問題 No.7 プライムナンバーゲーム
ユーザー nCk_cvnCk_cv
提出日時 2016-02-06 17:42:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 304 ms / 5,000 ms
コード長 1,217 bytes
コンパイル時間 2,128 ms
コンパイル使用メモリ 78,400 KB
実行使用メモリ 57,780 KB
最終ジャッジ日時 2024-04-09 03:55:23
合計ジャッジ時間 6,522 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
54,240 KB
testcase_01 AC 128 ms
54,200 KB
testcase_02 AC 209 ms
56,640 KB
testcase_03 AC 177 ms
55,888 KB
testcase_04 AC 165 ms
55,128 KB
testcase_05 AC 163 ms
55,428 KB
testcase_06 AC 209 ms
55,996 KB
testcase_07 AC 196 ms
55,548 KB
testcase_08 AC 181 ms
55,628 KB
testcase_09 AC 222 ms
56,164 KB
testcase_10 AC 130 ms
54,152 KB
testcase_11 AC 198 ms
55,516 KB
testcase_12 AC 258 ms
57,780 KB
testcase_13 AC 271 ms
56,024 KB
testcase_14 AC 304 ms
56,272 KB
testcase_15 AC 299 ms
56,088 KB
testcase_16 AC 289 ms
57,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.math.*;
import java.io.*;
 
public class Main {
	static ArrayList<Integer> pl;
	static int[][] memo;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		pl = new ArrayList<Integer>();
		boolean[] isntPrime = new boolean[n+1];
		pl.add(2);
		for(int i = 3; i <= n; i += 2) {
			if(!isntPrime[i]) pl.add(i);
			for(int j = i + i; j <= n; j += i) {
				isntPrime[j] = true;
			}
		}
		memo = new int[2][n+1];
		dfs(true,n);
		if(memo[0][n] == 1) System.out.println("Win");
		else System.out.println("Lose");
	}
	static boolean dfs(boolean a,int b) {
		if(a) {
			if(memo[0][b] != 0) return memo[0][b]==1?true:false;
			for(int i = 0; i < pl.size(); i++) {
				if(b - pl.get(i) <= 1) break;
				boolean ret = dfs(false,b - pl.get(i));
				if(ret) {
					memo[0][b] = 1;
					return true;
				}
			}
			memo[0][b] = -1;
			return false;
		}
		else {
			if(memo[1][b] != 0) return memo[1][b]==1?true:false;
			for(int i = 0; i < pl.size(); i++) {
				if(b - pl.get(i) <= 1) break;
				boolean ret = dfs(true,b - pl.get(i));
				if(!ret) {
					memo[1][b] = -1;
					return false;
				}
			}
			memo[1][b] = 1;
			return true;
		}
	}
	
}
0