結果

問題 No.7 プライムナンバーゲーム
ユーザー core123core123
提出日時 2019-04-17 16:03:22
言語 Java19
(openjdk 21)
結果
AC  
実行時間 300 ms / 5,000 ms
コード長 1,658 bytes
コンパイル時間 2,150 ms
コンパイル使用メモリ 77,276 KB
実行使用メモリ 59,756 KB
最終ジャッジ日時 2023-07-24 21:17:24
合計ジャッジ時間 6,826 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,020 KB
testcase_01 AC 119 ms
55,844 KB
testcase_02 AC 300 ms
59,324 KB
testcase_03 AC 205 ms
57,152 KB
testcase_04 AC 178 ms
55,916 KB
testcase_05 AC 180 ms
56,588 KB
testcase_06 AC 239 ms
59,212 KB
testcase_07 AC 219 ms
59,084 KB
testcase_08 AC 212 ms
59,008 KB
testcase_09 AC 253 ms
59,756 KB
testcase_10 AC 122 ms
55,556 KB
testcase_11 AC 220 ms
59,188 KB
testcase_12 AC 276 ms
59,084 KB
testcase_13 AC 279 ms
58,896 KB
testcase_14 AC 294 ms
59,068 KB
testcase_15 AC 282 ms
59,300 KB
testcase_16 AC 285 ms
59,316 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