"""
ID: ***
LANG: PYTHON3
TASK: pprime
"""
fin = open('pprime.in', 'r')
fout = open('pprime.out', 'w')
lower_Limit, upper_Limit = map(int, fin.readline().split())
maxL = len(str(upper_Limit))
minL = len(str(lower_Limit))
nums = [0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
str_Num = ""
enough = False
def isPrime(num):
for x in range(2, num):
if num % x == 0:
return False
return True
def generate_Palind(nums, chang, pos):
global enough
global str_Num
if enough:
return
lang = int((chang + 1) / 2)
if(pos > chang):
if int(str_Num) > upper_Limit:
enough = True
return
num = int(str_Num)
if (isPrime(num) and num >= lower_Limit):
fout.write("{}\n".format(num))
str_Num = str_Num[:lang]
return
if pos <= lang:
for i in range(1, 11):
if pos == 1 and nums[i] == 0:
continue
if chang == len(str(lower_Limit)) and int(str_Num + str(nums[i])) < int(str(lower_Limit)[:pos]):
continue
str_Num += "{}".format(nums[i])
generate_Palind(nums, chang, pos + 1)
str_Num = str_Num[:-1]
else:
str_Num += str_Num[chang - pos]
generate_Palind(nums, chang, pos + 1)
for chang in range(minL, maxL + 1):
generate_Palind(nums, chang, 1)
fout.close()
USACO 1.6 pprime Python3
最新推荐文章于 2022-05-25 15:27:44 发布