結果
問題 | No.7 プライムナンバーゲーム |
ユーザー | ぴろず |
提出日時 | 2014-12-21 00:33:20 |
言語 | Java19 (openjdk 21) |
結果 |
AC
|
実行時間 | 157 ms / 5,000 ms |
コード長 | 1,226 bytes |
コンパイル時間 | 2,128 ms |
コンパイル使用メモリ | 76,388 KB |
実行使用メモリ | 56,248 KB |
最終ジャッジ日時 | 2023-07-24 20:15:25 |
合計ジャッジ時間 | 5,868 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge15 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 155 ms
56,116 KB |
testcase_01 | AC | 154 ms
56,068 KB |
testcase_02 | AC | 156 ms
55,696 KB |
testcase_03 | AC | 155 ms
55,824 KB |
testcase_04 | AC | 156 ms
55,632 KB |
testcase_05 | AC | 153 ms
55,756 KB |
testcase_06 | AC | 154 ms
56,084 KB |
testcase_07 | AC | 155 ms
55,620 KB |
testcase_08 | AC | 155 ms
56,248 KB |
testcase_09 | AC | 152 ms
55,708 KB |
testcase_10 | AC | 154 ms
56,104 KB |
testcase_11 | AC | 154 ms
55,572 KB |
testcase_12 | AC | 153 ms
55,780 KB |
testcase_13 | AC | 154 ms
55,856 KB |
testcase_14 | AC | 156 ms
55,836 KB |
testcase_15 | AC | 157 ms
55,808 KB |
testcase_16 | AC | 154 ms
55,836 KB |
ソースコード
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; } }