結果

問題 No.7 プライムナンバーゲーム
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-04-03 07:34:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 159 ms / 5,000 ms
コード長 1,589 bytes
コンパイル時間 3,912 ms
コンパイル使用メモリ 79,200 KB
実行使用メモリ 54,500 KB
最終ジャッジ日時 2024-04-09 03:44:26
合計ジャッジ時間 7,381 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
54,048 KB
testcase_01 AC 143 ms
54,400 KB
testcase_02 AC 153 ms
54,288 KB
testcase_03 AC 156 ms
54,500 KB
testcase_04 AC 141 ms
54,340 KB
testcase_05 AC 144 ms
54,304 KB
testcase_06 AC 155 ms
54,308 KB
testcase_07 AC 152 ms
54,364 KB
testcase_08 AC 159 ms
54,312 KB
testcase_09 AC 155 ms
54,104 KB
testcase_10 AC 143 ms
54,312 KB
testcase_11 AC 150 ms
54,476 KB
testcase_12 AC 153 ms
54,164 KB
testcase_13 AC 150 ms
54,108 KB
testcase_14 AC 151 ms
54,448 KB
testcase_15 AC 157 ms
54,428 KB
testcase_16 AC 155 ms
54,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//No.7 プライムナンバーゲーム

import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;

public class No7 {
    
    static final Scanner sc = new Scanner(System.in);
    static final PrintWriter out = new PrintWriter(System.out,false);

    static void solve() {
        int n = sc.nextInt();
        int[] primes = sieveOfEratosthenes(n+1);
        boolean[] dp = new boolean[n+1];
        dp[0] = dp[1] = true;
        for (int i=2; i<=n; i++) {
            for (int j=0; j<primes.length; j++) {
                if (i < primes[j]) break;
                if (!dp[i-primes[j]]) {
                    dp[i] = true;
                    break;
                }
            }
        }
        out.println(dp[n]?"Win":"Lose");
    }

    static int[] sieveOfEratosthenes(int n) {
        if (n < 2) return null;
        boolean[] isPrime = new boolean[n];
        fill(isPrime,true);
        int[] ret = new int[n];
        int ptr = 0;
        isPrime[0] = isPrime[1] = false;
        for (int i=2; i<n; i++) {
            if (isPrime[i]) {
                ret[ptr++] = i;
                for (int j=i+i; j<n; j+=i) isPrime[j] = false;
            }
        }
        return Arrays.copyOfRange(ret,0,ptr);
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        solve();
        out.flush();

        long end = System.currentTimeMillis();
        //trace(end-start + "ms");
        sc.close();
    }

    static void trace(Object... o) { System.out.println(deepToString(o));}
}
0