解题思路:
注意事项:
参考代码:import math
def get_primes(max_val):
v = [True] * (max_val + 1)
for x in range(2, max_val + 1):
if not v[x]:
continue
for y in range(x * 2, max_val + 1, x):
v[y] = False
primes = [x for x in range(2, max_val + 1) if v[x]]
return primes
def win(x):
if memo[x] != 0:
return memo[x] == 1
if x <= 1:
return False
for i in range(bis(x), -1, -1):
if not win(x - primes[i]):
memo[x] = 1
return True
memo[x] = -1
return False
def bis(x):
l = 0
r = len(primes)
while l < r:
m = (l + r) // 2
if primes[m] <= x:
l = m + 1
else:
r = m
return l-1
T = int(input())
woods = [int(input()) for _ in range(T)]
max_wood = max(woods)
primes = get_primes(max_wood)
memo = [0] * (max_wood + 1)
for wood in woods:
result = 1 if win(wood) else 0
print(result)
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题6.11 (C语言代码)浏览:565 |
WU-链表数据求和操作 (C++代码)浏览:1382 |
1009题解浏览:802 |
Cylinder (C语言描述+详细分析)浏览:3375 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:592 |
剪刀石头布 (C++代码)浏览:1811 |
C语言程序设计教程(第三版)课后习题10.3 (C语言代码)浏览:523 |
整数平均值 (C语言代码)浏览:856 |
简单的a+b (C语言代码)浏览:600 |
简单的a+b (C语言代码)浏览:491 |