結果

問題 No.7 プライムナンバーゲーム
ユーザー fujisufujisu
提出日時 2015-02-02 23:40:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 382 ms / 5,000 ms
コード長 2,933 bytes
コンパイル時間 3,061 ms
コンパイル使用メモリ 78,904 KB
実行使用メモリ 55,108 KB
最終ジャッジ日時 2024-04-09 03:42:10
合計ジャッジ時間 6,901 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
50,416 KB
testcase_01 AC 52 ms
50,128 KB
testcase_02 AC 129 ms
53,308 KB
testcase_03 AC 87 ms
51,928 KB
testcase_04 AC 88 ms
51,104 KB
testcase_05 AC 80 ms
51,424 KB
testcase_06 AC 181 ms
53,196 KB
testcase_07 AC 153 ms
53,092 KB
testcase_08 AC 120 ms
52,852 KB
testcase_09 AC 220 ms
53,064 KB
testcase_10 AC 54 ms
50,476 KB
testcase_11 AC 155 ms
53,012 KB
testcase_12 AC 286 ms
53,200 KB
testcase_13 AC 299 ms
53,416 KB
testcase_14 AC 382 ms
55,108 KB
testcase_15 AC 363 ms
53,436 KB
testcase_16 AC 337 ms
54,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.List;

public class Main {
	List<Integer> list;
	int[] dp;

	int dp(int k) {
		if (k < 2) {
			return 1;
		}
		if (dp[k] != 0) {
			return dp[k];
		}

		int res = -1;
		for (Integer m : list) {
			if (0 <= k - m) {
				if (dp(k - m) == -1) {
					res = 1;
					break;
				}
			} else {
				break;
			}
		}
		return dp[k] = res;
	}

	void run() {
		MyScanner sc = new MyScanner();
		int max = 10001;
		boolean[] prime = new boolean[max];
		Arrays.fill(prime, true);
		prime[0] = prime[1] = false;
		for (int i = 0; i * i <= max; i++) {
			if (prime[i]) {
				for (int j = i * i; j < max; j += i) {
					prime[j] = false;
				}
			}
		}

		list = new LinkedList<Integer>();
		for (int i = 0; i < max; i++) {
			if (prime[i]) {
				list.add(i);
			}
		}

		dp = new int[max];
		System.out.println(dp(sc.nextInt()) > 0 ? "Win" : "Lose");
	}

	public static void main(String[] args) {
		new Main().run();
	}

	public void mapDebug(int[][] a) {
		System.out.println("--------map display---------");
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%3d ", a[i][j]);
			}
			System.out.println();
		}
		System.out.println("----------------------------" + '\n');
	}

	class MyScanner {
		int read() {
			try {
				return System.in.read();
			} catch (IOException e) {
				throw new InputMismatchException();
			}
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextIntArray(int n) {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();
			return array;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		long[] nextLongArray(int n) {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();
			return array;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		double[] nextDoubleArray(int n) {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();
			return array;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] nextStringArray(int n) {
			String[] array = new String[n];
			for (int i = 0; i < n; i++)
				array[i] = next();

			return array;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}
0