Digprime.py — различия между версиями
Материал из DISCOPAL
					
										
					
					StasFomin (обсуждение | вклад)  | 
				StasFomin (обсуждение | вклад)   | 
				||
| Строка 35: | Строка 35: | ||
     if dp[i][taken][ls] != -1:  |      if dp[i][taken][ls] != -1:  | ||
| − |          return   | + |          return dp[i][taken][ls]  | 
     if ls and taken:  |      if ls and taken:  | ||
| Строка 48: | Строка 48: | ||
         newtaken = taken or (x == 2) or (x == 3) or (x == 5) or (x == 7)  |          newtaken = taken or (x == 2) or (x == 3) or (x == 5) or (x == 7)  | ||
         newls = ls or (x != limit)  |          newls = ls or (x != limit)  | ||
| − |          res += calculate(  | + |         newi = i + 1  | 
| + |          res += calculate(newi, newtaken, newls)  | ||
| − |      dp[i][taken][ls] =   | + |      dp[i][taken][ls] = res  | 
| − |      return   | + |      return res  | 
Версия 02:13, 1 мая 2022
import sys digits = 19 # globals input_str = "" dp = [[[-1, -1], [-1, -1]] for i in range(digits)] N = None def get_input(): lines = sys.stdin.readlines() content = " ".join(lines).strip() for token in content.split(): yield token tc_ = get_input() next(tc_) p10 = [10**i for i in range(digits)] def calculate(i, taken: int, ls: int) -> int: ''' i — индекс цифры taken — встречали ли простые цифры раньше ls — в текущем индексе можно делать полный перебор ''' global input_str global dp global N if i == N: return taken if dp[i][taken][ls] != -1: return dp[i][taken][ls] if ls and taken: return p10[N - i] res = 0 if ls: limit = 9 else: limit = int(input_str[i]) for x in range(limit + 1): newtaken = taken or (x == 2) or (x == 3) or (x == 5) or (x == 7) newls = ls or (x != limit) newi = i + 1 res += calculate(newi, newtaken, newls) dp[i][taken][ls] = res return res for input_str in tc_: N = len(input_str) for j in range(digits): dp[j][0][0] = -1 dp[j][1][0] = -1 dp[j][1][1] = -1 dp[j][0][1] = -1 print(calculate(0, 0, 0))