結果

問題 No.7 プライムナンバーゲーム
ユーザー takeya_okinotakeya_okino
提出日時 2019-09-14 23:22:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 197 ms / 5,000 ms
コード長 858 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 78,004 KB
実行使用メモリ 54,424 KB
最終ジャッジ日時 2024-04-09 04:42:46
合計ジャッジ時間 5,906 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
53,984 KB
testcase_01 AC 130 ms
54,204 KB
testcase_02 AC 196 ms
54,280 KB
testcase_03 AC 148 ms
54,228 KB
testcase_04 AC 145 ms
54,356 KB
testcase_05 AC 145 ms
54,092 KB
testcase_06 AC 158 ms
54,176 KB
testcase_07 AC 153 ms
54,260 KB
testcase_08 AC 147 ms
54,224 KB
testcase_09 AC 162 ms
54,156 KB
testcase_10 AC 131 ms
54,036 KB
testcase_11 AC 149 ms
54,424 KB
testcase_12 AC 180 ms
54,212 KB
testcase_13 AC 183 ms
54,136 KB
testcase_14 AC 197 ms
54,036 KB
testcase_15 AC 192 ms
54,016 KB
testcase_16 AC 188 ms
54,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] dp = new int[n + 1];
    ArrayList<Integer> prime = new ArrayList<Integer>();
    boolean[] is_prime = new boolean[n - 1];
    for(int i = 0; i <= n - 2; i++) is_prime[i] = true;
    is_prime[0] = false;
    if(n > 2) is_prime[1] = false;
    for(int i = 2; i <= n - 2; i++) {
      if(is_prime[i]) {
        prime.add(i);
        for(int j = 2 * i; j <= n - 2; j += i) is_prime[j] = false;
      }
    }
    for(int i = 2; i < n + 1; i++) {
      for(int j = 0; j < prime.size(); j++) {
        int p = prime.get(j);
        if(p < i - 1) {
          if(dp[i - p] == 0) dp[i] = 1;
        }
      }
    }
    String ans = "Win";
    if(dp[n] == 0) ans = "Lose";
    System.out.println(ans);
  }
}
0