結果

問題 No.7 プライムナンバーゲーム
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-02-24 10:50:05
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,399 bytes
コンパイル時間 3,313 ms
コンパイル使用メモリ 81,940 KB
実行使用メモリ 60,324 KB
最終ジャッジ日時 2023-08-23 07:10:18
合計ジャッジ時間 8,661 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
55,984 KB
testcase_01 AC 133 ms
55,780 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 166 ms
56,080 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 239 ms
56,156 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 275 ms
58,432 KB
testcase_15 AC 262 ms
58,248 KB
testcase_16 WA -
権限があれば一括ダウンロードができます

ソースコード

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[prime] = dp[prime] | dp[i - prime];
            }
        }
        
        String ans = dp[n] ? "Win" : "Lose";
        System.out.println(ans);
    }
}
0