結果

問題 No.7 プライムナンバーゲーム
ユーザー mastersatoshimastersatoshi
提出日時 2015-08-01 17:25:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 92 ms / 5,000 ms
コード長 1,516 bytes
コンパイル時間 2,024 ms
コンパイル使用メモリ 78,468 KB
実行使用メモリ 53,320 KB
最終ジャッジ日時 2024-04-09 03:48:26
合計ジャッジ時間 3,987 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
50,332 KB
testcase_01 AC 49 ms
50,384 KB
testcase_02 AC 83 ms
51,560 KB
testcase_03 AC 68 ms
51,240 KB
testcase_04 AC 56 ms
50,312 KB
testcase_05 AC 55 ms
50,284 KB
testcase_06 AC 79 ms
51,468 KB
testcase_07 AC 77 ms
51,440 KB
testcase_08 AC 74 ms
53,320 KB
testcase_09 AC 80 ms
51,600 KB
testcase_10 AC 53 ms
50,356 KB
testcase_11 AC 83 ms
51,444 KB
testcase_12 AC 82 ms
51,784 KB
testcase_13 AC 92 ms
51,728 KB
testcase_14 AC 84 ms
51,552 KB
testcase_15 AC 82 ms
51,604 KB
testcase_16 AC 82 ms
51,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());

        int sc = (int) Math.sqrt(N);

        int[] d = new int[N + 1];

        d[0] = -1;
        d[1] = -1;

        for (int i = 2; i <= sc; i++) {
            if (d[i] != 0) {
                continue;
            }
            for (int j = i; j < N; j++) {
                int dest = i * j;
                if (dest > d.length - 1) {
                    break;
                }
                d[i * j] = -1;
            }
        }

        ArrayList<Integer> prime = new ArrayList();

        for (int i = 0; i < N; i++) {
            if (d[i] != -1) {
                prime.add(i);
            }
        }

        int[] dq = new int[N + 1];

        Arrays.fill(dq, Integer.MAX_VALUE);
        dq[0] = 0;
        dq[1] = 0;

        for (int i = 2; i <= N; i++) {
            if (dq[i] != Integer.MAX_VALUE) {
                continue;
            }

            dq[i] = 1;

            for (int j = 0; j < prime.size(); j++) {

                if(i + prime.get(j) <= N && dq[i + prime.get(j)] == Integer.MAX_VALUE){
                    dq[i + prime.get(j)] = 0;
                }
            }
        }

        if (dq[N] == 1) {
            System.out.println("Lose");
        } else {
            System.out.println("Win");
        }

    }

}
0