結果

問題 No.7 プライムナンバーゲーム
ユーザー yuki2006yuki2006
提出日時 2014-10-04 02:41:57
言語 Python2
(2.7.18)
結果
AC  
実行時間 809 ms / 5,000 ms
コード長 1,274 bytes
コンパイル時間 141 ms
コンパイル使用メモリ 7,040 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 03:39:24
合計ジャッジ時間 6,430 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
6,816 KB
testcase_01 AC 12 ms
6,948 KB
testcase_02 AC 809 ms
6,948 KB
testcase_03 AC 62 ms
6,944 KB
testcase_04 AC 25 ms
6,944 KB
testcase_05 AC 25 ms
6,948 KB
testcase_06 AC 242 ms
6,948 KB
testcase_07 AC 167 ms
6,944 KB
testcase_08 AC 81 ms
6,948 KB
testcase_09 AC 361 ms
6,948 KB
testcase_10 AC 11 ms
6,944 KB
testcase_11 AC 167 ms
6,948 KB
testcase_12 AC 581 ms
6,948 KB
testcase_13 AC 617 ms
6,944 KB
testcase_14 AC 802 ms
6,944 KB
testcase_15 AC 758 ms
6,948 KB
testcase_16 AC 702 ms
6,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-

def eratos(n):
    list = xrange(2, n + 1)
    ans = []
    while len(list):
        ans.append(list[0])
        list = filter(lambda x: x % list[0] > 0, list)
    return ans


def eratos2(n):
    table = [True] * (n + 1)
    table[0] = table[1] = False
    a = 2
    ans = []
    while a <= n:
        if table[a]:
            ans.append(a)
            k = 2
            while a * k <= n:
                table[a * k] = False
                k += 1
        a += 1

    return ans


def solve(N):
    prime_map = eratos2(N)

    memo = [None] * (N + 1)
    dp = [False] * (N + 1)

    # Trueなら相手の負け
    def dfs(n):
        if memo[n] is not None:
            return memo[n]

        for p in filter(lambda x: x <= n, prime_map):
            if 0 <= n - p <= 1:
                continue
            if not dfs(n - p):
                memo[n] = True
                return memo[n]
        memo[n] = False
        return memo[n]

    def on_dp(N):
        dp[0] = True
        dp[1] = True
        for i in xrange(2, N + 1):
            for p in prime_map:
                if p > i: break
                dp[i] |= not dp[i - p]
        return dp[N]
    print "Win" if on_dp(N) else "Lose"

solve(int(raw_input()))
    # solve(10000) # Win
0