結果

問題 No.7 プライムナンバーゲーム
ユーザー tentententen
提出日時 2024-02-05 13:38:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 101 ms / 5,000 ms
コード長 1,787 bytes
コンパイル時間 2,369 ms
コンパイル使用メモリ 79,212 KB
実行使用メモリ 55,288 KB
最終ジャッジ日時 2024-02-05 13:38:49
合計ジャッジ時間 5,013 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
53,988 KB
testcase_01 AC 53 ms
53,976 KB
testcase_02 AC 91 ms
55,272 KB
testcase_03 AC 81 ms
55,260 KB
testcase_04 AC 67 ms
54,368 KB
testcase_05 AC 65 ms
54,372 KB
testcase_06 AC 85 ms
55,268 KB
testcase_07 AC 83 ms
55,280 KB
testcase_08 AC 80 ms
55,244 KB
testcase_09 AC 83 ms
53,304 KB
testcase_10 AC 54 ms
53,984 KB
testcase_11 AC 81 ms
55,288 KB
testcase_12 AC 88 ms
55,280 KB
testcase_13 AC 88 ms
55,272 KB
testcase_14 AC 90 ms
55,288 KB
testcase_15 AC 91 ms
55,272 KB
testcase_16 AC 101 ms
55,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        boolean[] isNotPrimes = new boolean[n + 1];
        List<Integer> primes = new ArrayList<>();
        for (int i = 2; i <= n; i++) {
            if (!isNotPrimes[i]) {
                primes.add(i);
                for (int j = 2; j * i <= n; j++) {
                    isNotPrimes[j * i] = true;
                }
            }
        }
        boolean[] dp = new boolean[n + 1];
        dp[0] = true;
        dp[1] = true;
        for (int i = 2; i <= n; i++) {
            for (int j = 0; j < primes.size() && primes.get(j) <= i && !dp[i]; j++) {
                dp[i] = !dp[i - primes.get(j)];
            }
        }
        System.out.println(dp[n] ? "Win" : "Lose");
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0