結果

問題 No.7 プライムナンバーゲーム
ユーザー uafr_csuafr_cs
提出日時 2015-06-03 14:13:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 170 ms / 5,000 ms
コード長 960 bytes
コンパイル時間 2,442 ms
コンパイル使用メモリ 77,948 KB
実行使用メモリ 54,508 KB
最終ジャッジ日時 2024-04-09 03:46:29
合計ジャッジ時間 6,036 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
54,336 KB
testcase_01 AC 133 ms
54,220 KB
testcase_02 AC 167 ms
54,232 KB
testcase_03 AC 143 ms
53,744 KB
testcase_04 AC 139 ms
54,000 KB
testcase_05 AC 142 ms
54,508 KB
testcase_06 AC 157 ms
54,432 KB
testcase_07 AC 155 ms
54,340 KB
testcase_08 AC 155 ms
54,424 KB
testcase_09 AC 163 ms
54,504 KB
testcase_10 AC 131 ms
54,252 KB
testcase_11 AC 170 ms
54,412 KB
testcase_12 AC 161 ms
54,116 KB
testcase_13 AC 164 ms
54,436 KB
testcase_14 AC 167 ms
54,212 KB
testcase_15 AC 164 ms
54,340 KB
testcase_16 AC 168 ms
54,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
	
		final int N = sc.nextInt();
		boolean[] is_prime = new boolean[N + 1];
		Arrays.fill(is_prime, true);
		is_prime[0] = is_prime[1] = false;
		
		List<Integer> primes = new LinkedList<Integer>();
		
		for(int i = 2; i <= N; i++){
			if(is_prime[i]){
				primes.add(i);
				
				for(int j = i * 2; j <= N; j += i){
					is_prime[j] = false;
				}
			}
		}
		
		boolean[] DP = new boolean[N + 1];
		DP[0] = DP[1] = true;
		for(int i = 2; i <= N; i++){
			boolean only_win = true;
			
			for(final int prime : primes){
				if(prime > i){ break; }
				
				if(!DP[i - prime]){
					only_win = false;
					break;
				}
			}
			
			DP[i] = !only_win;
		}
		
		//System.out.println(Arrays.toString(DP));
		
		System.out.println(DP[N] ? "Win" : "Lose");
	}
	
}
0