結果

問題 No.7 プライムナンバーゲーム
ユーザー core123core123
提出日時 2019-04-17 16:03:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 308 ms / 5,000 ms
コード長 1,658 bytes
コンパイル時間 2,409 ms
コンパイル使用メモリ 80,100 KB
実行使用メモリ 58,144 KB
最終ジャッジ日時 2024-04-09 04:37:13
合計ジャッジ時間 7,351 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
53,892 KB
testcase_01 AC 123 ms
54,172 KB
testcase_02 AC 307 ms
57,848 KB
testcase_03 AC 206 ms
54,660 KB
testcase_04 AC 176 ms
54,396 KB
testcase_05 AC 178 ms
54,644 KB
testcase_06 AC 239 ms
57,728 KB
testcase_07 AC 234 ms
57,548 KB
testcase_08 AC 218 ms
55,568 KB
testcase_09 AC 252 ms
57,336 KB
testcase_10 AC 125 ms
54,004 KB
testcase_11 AC 244 ms
57,688 KB
testcase_12 AC 270 ms
58,120 KB
testcase_13 AC 281 ms
58,140 KB
testcase_14 AC 308 ms
58,144 KB
testcase_15 AC 297 ms
57,672 KB
testcase_16 AC 289 ms
58,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.util.stream.*;
import static java.lang.Math.*;
public class Main{
	public static void main(String... args){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		boolean[] isPrime=new boolean[n+1];
		Arrays.fill(isPrime,true);
		isPrime[0]=false;
		isPrime[1]=false;
		for(int i=2;i<isPrime.length;i++){
			for(int j=i*2;j<isPrime.length;j+=i){
				isPrime[j]=false;
			}
		}
		List<Integer> primes=new ArrayList<>();
		for(int i=0;i<isPrime.length;i++){
			if(isPrime[i]) primes.add(i);
		}
		long[] grundy=new long[n+1];
		Arrays.fill(grundy,-1);
		grundy[2]=0;
		grundy[3]=0;
		for(int i=4;i<grundy.length;i++){
			Set<Long> set=new HashSet<>();
			for(int j=0;j<primes.size();j++){
				int index=i-primes.get(j);
				if(index<0)break;
				set.add(grundy[index]);
			}
			for(long j=0;j<set.size()+1;j++){
				if(!set.contains(j)){
					grundy[i]=j;
					break;
				}
			}
			
		}
		if(grundy[n]==0){
			put("Lose");
		}else{
			put("Win");
		}
		
	}
	public static class Pair{
		final int x,y;
		Pair(int x,int y){
			this.x=x;
			this.y=y;
		}
		@Override
		public String toString(){
			return String.format("Pair(%d,%d)",x,y);
		}
	}
	public static void print(Object object){
        System.out.print(object);
    }
    public static void put(Object object) {
        System.out.println(object);
    }
    public static void put(){
        System.out.println();
    }
 
    public static void print(String format,Object... args){
        System.out.print(String.format(format,args));
    }
    public static void put(String format,Object... args) {
        System.out.println(String.format(format,args));
    }
}
0