結果

問題 No.7 プライムナンバーゲーム
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-06 20:23:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 183 ms / 5,000 ms
コード長 1,012 bytes
コンパイル時間 244 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-09 03:48:29
合計ジャッジ時間 2,428 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,880 KB
testcase_01 AC 28 ms
10,752 KB
testcase_02 AC 183 ms
10,880 KB
testcase_03 AC 39 ms
10,752 KB
testcase_04 AC 30 ms
10,880 KB
testcase_05 AC 30 ms
10,752 KB
testcase_06 AC 73 ms
10,880 KB
testcase_07 AC 60 ms
10,880 KB
testcase_08 AC 43 ms
10,880 KB
testcase_09 AC 98 ms
10,880 KB
testcase_10 AC 29 ms
10,880 KB
testcase_11 AC 60 ms
10,880 KB
testcase_12 AC 140 ms
11,008 KB
testcase_13 AC 146 ms
11,008 KB
testcase_14 AC 183 ms
11,008 KB
testcase_15 AC 174 ms
10,880 KB
testcase_16 AC 163 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def primes2(limit):
    ''' returns a list of prime numbers upto limit.
    source: Rossetta code: Sieve of Eratosthenes
    http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Odds-only_version_of_the_array_sieve_above
    '''
    if limit < 2: return []
    if limit < 3: return [2]
    lmtbf = (limit - 3) // 2
    buf = [True] * (lmtbf + 1)
    for i in range((int(limit ** 0.5) - 3) // 2 + 1):
        if buf[i]:
            p = i + i + 3
            s = p * (i + 1) + i
            buf[s::p] = [False] * ((lmtbf - s) // p + 1)
    return [2] + [i + i + 3 for i, v in enumerate(buf) if v]

def solve(N):
    primes = primes2(N)
    dp = [-1] * (N + 1)
    dp[0] = 1
    dp[1] = 1
    for n in range(2, N + 1):
        for p in primes:
            if p > n:
                dp[n] = 0
                break
            if dp[n - p] == 0:
                dp[n] = 1
                break
        else:
            dp[n] = 0
    return dp[N]

N = int(input())
if solve(N):
    print('Win')
else:
    print('Lose')
0