Digprime.py — различия между версиями
Материал из DISCOPAL
					
										
					
					StasFomin (обсуждение | вклад)  | 
				StasFomin (обсуждение | вклад)   | 
				||
| (не показаны 3 промежуточные версии этого же участника) | |||
| Строка 30: | Строка 30: | ||
     global dp  |      global dp  | ||
     global N  |      global N  | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
     res = 0  |      res = 0  | ||
| Строка 45: | Строка 36: | ||
     else:  |      else:  | ||
         limit = int(input_str[i])  |          limit = int(input_str[i])  | ||
| + | |||
     for x in range(limit + 1):  |      for x in range(limit + 1):  | ||
         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)  | ||
| − | + |          newi = i + 1  | |
| + | |||
| + |         if newi == N:  | ||
| + |             ret = newtaken  | ||
| + |         elif newls and newtaken:  | ||
| + |             ret = p10[N - newi]  | ||
| + |         else:  | ||
| + |             cached = dp[newi][newtaken][newls]  | ||
| + |             if cached != -1:  | ||
| + |                 ret = cached  | ||
| + |             else:  | ||
| + |                 ret = calculate(newi, newtaken, newls)  | ||
| + | |||
| + |         res += ret  | ||
| − |      dp[i][taken][ls] =   | + |      dp[i][taken][ls] = res  | 
| − |      return   | + |      return res  | 
Текущая версия на 02:33, 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 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 if newi == N: ret = newtaken elif newls and newtaken: ret = p10[N - newi] else: cached = dp[newi][newtaken][newls] if cached != -1: ret = cached else: ret = calculate(newi, newtaken, newls) res += ret 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))