📅  最后修改于: 2023-12-03 15:25:15.649000             🧑  作者: Mango
题目描述:输入一个正整数N,计算N能表示为质数和平方的两倍的方式数量。
def count_ways(N: int) -> int:
prime = [True] * (N+1)
cnt = 0
# 预处理质数
prime[0] = prime[1] = False
for i in range(2, N+1):
if prime[i]:
cnt += 1
for j in range(i*i, N+1, i):
prime[j] = False
# 枚举质数和平方的次数
for i in range(2, N+1, 2):
for p in range(2, N+1):
if prime[p]:
for j in range(1, N+1):
if i + 2 * p * j * j == N:
cnt += 1
return cnt
print(count_ways(6)) # 1
print(count_ways(50)) # 1
print(count_ways(100)) # 2
print(count_ways(500)) # 15