結果

問題 No.7 プライムナンバーゲーム
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-02-24 10:56:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 285 ms / 5,000 ms
コード長 1,408 bytes
コンパイル時間 2,737 ms
コンパイル使用メモリ 83,396 KB
実行使用メモリ 56,808 KB
最終ジャッジ日時 2024-04-09 04:34:55
合計ジャッジ時間 7,232 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 137 ms
54,088 KB
testcase_01 AC 136 ms
54,144 KB
testcase_02 AC 273 ms
56,804 KB
testcase_03 AC 178 ms
54,204 KB
testcase_04 AC 171 ms
54,384 KB
testcase_05 AC 175 ms
54,320 KB
testcase_06 AC 202 ms
54,396 KB
testcase_07 AC 192 ms
54,556 KB
testcase_08 AC 184 ms
54,388 KB
testcase_09 AC 234 ms
54,744 KB
testcase_10 AC 139 ms
53,920 KB
testcase_11 AC 196 ms
54,396 KB
testcase_12 AC 247 ms
56,436 KB
testcase_13 AC 257 ms
56,808 KB
testcase_14 AC 285 ms
56,756 KB
testcase_15 AC 271 ms
56,592 KB
testcase_16 AC 269 ms
56,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int n = stdin.nextInt();
        
        // エラトステネスの篩: n以下の素数をすべて求める。
        boolean[] isPrime = new boolean[n + 1];
        Arrays.fill(isPrime, true);
        isPrime[0] = isPrime[1] = false;
        for (int i = 0; i * i <= n; i++) {
            if (!isPrime[i]) continue;
            for (int j = i + i; j <= n; j += i) isPrime[j] = false;
        }
        TreeSet<Integer> primes = IntStream.range(0, n + 1)
                                            .filter(i -> isPrime[i])
                                            .boxed()
                                            .collect(Collectors.toCollection(TreeSet::new));
        
        boolean[] dp = new boolean[n + 1];
        dp[0] = dp[1] = true; // n=0またはn=1ならば先行勝利
        for (int i = 2; i <= n; i++) {
            // 遷移前が負け(=後攻が負け)の場合は選考が勝利
            for (int prime : primes.subSet(0, i + 1)) {
                dp[i] = dp[i] | !dp[i - prime];
            }
        }
        
        String ans = dp[n] ? "Win" : "Lose";
        System.out.println(ans);
    }
}
0