結果

問題 No.7 プライムナンバーゲーム
ユーザー tsunabittsunabit
提出日時 2019-08-15 11:31:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 173 ms / 5,000 ms
コード長 1,565 bytes
コンパイル時間 3,552 ms
コンパイル使用メモリ 77,664 KB
実行使用メモリ 54,476 KB
最終ジャッジ日時 2024-04-09 04:42:14
合計ジャッジ時間 7,212 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
53,916 KB
testcase_01 AC 124 ms
53,948 KB
testcase_02 AC 169 ms
54,008 KB
testcase_03 AC 141 ms
54,340 KB
testcase_04 AC 137 ms
54,196 KB
testcase_05 AC 134 ms
53,928 KB
testcase_06 AC 145 ms
54,144 KB
testcase_07 AC 140 ms
54,476 KB
testcase_08 AC 140 ms
54,336 KB
testcase_09 AC 155 ms
54,244 KB
testcase_10 AC 131 ms
54,128 KB
testcase_11 AC 142 ms
54,416 KB
testcase_12 AC 163 ms
53,976 KB
testcase_13 AC 162 ms
54,060 KB
testcase_14 AC 169 ms
53,980 KB
testcase_15 AC 168 ms
54,316 KB
testcase_16 AC 173 ms
54,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.math.*;

public class No7 {
	static int count = 0;

	public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	// ここで一度だけ素数判定をすれば計算量が減る
    	int[] p = new int[n];
    	p = ar_prime(n);
    	
    	String[] memo = new String[n];
    	memo[0] = "後手必勝";
    	for(int i = 1; i < n; i++) {
    		for(int j = p.length-1; j >= 0; j--) {
    			int t = (i+1) - p[j];
    			if(t <= 1) {
    				continue;
    			}else if(memo[t-1] == "後手必勝") {
    				memo[i] = "先手必勝";
    				continue;
    			}
    		}
    		if(memo[i] != "先手必勝") {
				memo[i] = "後手必勝";
			}
    	}
    	if(memo[n-1] == "先手必勝") System.out.println("Win");
    	else System.out.println("Lose");
    }
	public static int[] ar_prime(int num) {
		List<Integer> al = new ArrayList<Integer>();
    	for(int i = 0; i < num; i++) if(isPrime(i + 1)) al.add(i + 1);
    	int[] p = new int[al.size()];
    	int count = 0;
    	for(int v : al) {
    		p[count] = v;
    		count++;
    	}
    	return p;
    }
    public static boolean isPrime(int num) {
        if (num < 2) return false;
        else if (num == 2) return true;
        else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

        double sqrtNum = Math.sqrt(num);
        for (int i = 3; i <= sqrtNum; i += 2) {
            if (num % i == 0) return false;	//素数でない
        }
        return true;			// 素数である
    }
}
0