Digprime.py — различия между версиями
Материал из DISCOPAL
					
										
					
					StasFomin (обсуждение | вклад)  | 
				StasFomin (обсуждение | вклад)   | 
				||
| Строка 6: | Строка 6: | ||
input_str = ""  | input_str = ""  | ||
dp = [[[-1, -1], [-1, -1]] for i in range(25)]  | dp = [[[-1, -1], [-1, -1]] for i in range(25)]  | ||
| + | N = None  | ||
| Строка 28: | Строка 29: | ||
     global input_str  |      global input_str  | ||
     global dp  |      global dp  | ||
| − |      if i ==   | + |     global N  | 
| + | |||
| + |      if i == N:  | ||
         return taken  |          return taken  | ||
| + | |||
     if dp[i][taken][ls] != -1:  |      if dp[i][taken][ls] != -1:  | ||
         return int(dp[i][taken][ls])  |          return int(dp[i][taken][ls])  | ||
     if ls and taken:  |      if ls and taken:  | ||
| − |          return p10[  | + |          return p10[N - i]  | 
     res = 0  |      res = 0  | ||
| Строка 49: | Строка 53: | ||
for input_str in tc_:  | for input_str in tc_:  | ||
| + |     N = len(input_str)  | ||
     for j in range(digits):  |      for j in range(digits):  | ||
         dp[j][0][0] = -1  |          dp[j][0][0] = -1  | ||
Версия 01:50, 1 мая 2022
import sys digits = 19 # globals input_str = "" dp = [[[-1, -1], [-1, -1]] for i in range(25)] 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 int(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): res += calculate(i + 1, taken | (x == 2) | (x == 3) | (x == 5) | (x == 7), ls | (x != limit)) dp[i][taken][ls] = int(res) return int(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))