結果

問題 No.7 プライムナンバーゲーム
ユーザー tentententen
提出日時 2022-07-28 16:18:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 85 ms / 5,000 ms
コード長 1,661 bytes
コンパイル時間 2,234 ms
コンパイル使用メモリ 74,920 KB
実行使用メモリ 52,448 KB
最終ジャッジ日時 2023-09-25 08:39:14
合計ジャッジ時間 4,434 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,908 KB
testcase_01 AC 45 ms
49,280 KB
testcase_02 AC 81 ms
51,816 KB
testcase_03 AC 71 ms
51,612 KB
testcase_04 AC 57 ms
49,384 KB
testcase_05 AC 55 ms
49,460 KB
testcase_06 AC 74 ms
51,872 KB
testcase_07 AC 72 ms
52,208 KB
testcase_08 AC 72 ms
51,900 KB
testcase_09 AC 77 ms
51,784 KB
testcase_10 AC 46 ms
47,236 KB
testcase_11 AC 73 ms
51,668 KB
testcase_12 AC 79 ms
52,448 KB
testcase_13 AC 78 ms
51,832 KB
testcase_14 AC 85 ms
51,880 KB
testcase_15 AC 83 ms
52,000 KB
testcase_16 AC 79 ms
50,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

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];
        ArrayList<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[] wins = new boolean[n + 1];
        wins[0] = true;
        wins[1] = true;
        for (int i = 2; i <= n; i++) {
            for (int j = 0; j < primes.size() && primes.get(j) <= i && !wins[i]; j++) {
                wins[i] |= !wins[i - primes.get(j)];
            }
        }
        if (wins[n]) {
            System.out.println("Win");
        } else {
            System.out.println("Lose");
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0