結果

問題 No.7 プライムナンバーゲーム
ユーザー ぴろずぴろず
提出日時 2014-12-21 00:33:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 157 ms / 5,000 ms
コード長 1,226 bytes
コンパイル時間 2,136 ms
コンパイル使用メモリ 78,988 KB
実行使用メモリ 54,340 KB
最終ジャッジ日時 2024-04-09 03:41:15
合計ジャッジ時間 5,589 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 155 ms
54,304 KB
testcase_01 AC 154 ms
54,304 KB
testcase_02 AC 154 ms
54,156 KB
testcase_03 AC 153 ms
53,928 KB
testcase_04 AC 155 ms
54,196 KB
testcase_05 AC 156 ms
54,332 KB
testcase_06 AC 156 ms
54,264 KB
testcase_07 AC 156 ms
54,208 KB
testcase_08 AC 157 ms
54,056 KB
testcase_09 AC 157 ms
54,240 KB
testcase_10 AC 156 ms
54,328 KB
testcase_11 AC 156 ms
54,340 KB
testcase_12 AC 145 ms
53,648 KB
testcase_13 AC 155 ms
54,140 KB
testcase_14 AC 155 ms
54,300 KB
testcase_15 AC 157 ms
53,868 KB
testcase_16 AC 157 ms
54,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no007;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Integer> prime = Sieve.primeList(10000);
		int[] dp = new int[10001];
		dp[0] = dp[1] = 1;
		for(int i=2;i<=10000;i++) {
			boolean win = false;
			for(int p:prime) {
				if (i < p) {
					break;
				}
				if (dp[i-p] ==0) {
					win = true;
					break;
				}
			}
			if (win) {
				dp[i] = 1;
			}
		}
		//System.out.println(Arrays.toString(dp));
		System.out.println(dp[n] == 1 ? "Win" : "Lose");
	}

}

class Sieve {
	public static boolean[] isPrimeArray(int max) {
		boolean[] isPrime = new boolean[max+1];
		Arrays.fill(isPrime, true);
		isPrime[0] = isPrime[1] = false;
		for(int i=2;i*i<=max;i++) {
			if (isPrime[i]) {
				int j = i * 2;
				while(j<=max) {
					isPrime[j] = false;
					j += i;
				}
			}
		}
		return isPrime;
	}
	public static ArrayList<Integer> primeList(int max) {
		boolean[] isPrime = isPrimeArray(max);
		ArrayList<Integer> primeList = new ArrayList<>();
		for(int i=2;i<=max;i++) {
			if (isPrime[i]) {
				primeList.add(i);
			}
		}
		return primeList;
	}
}
0