- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 93 lines of Python from the credited upstream file abc250_d.py.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4class Prime:5 '''Represents a snippet for prime numbers.6 '''7 8 def __init__(self, number):9 self.number = number10 self._values = []11 12 def is_included(self) -> bool:13 '''Determine whether it is a prime number.14 Args:15 number: Int of number (greater than 0).16 Returns:17 True if the input number was prime.18 False if the input number was not prime.19 See:20 https:qiita.com/srtk86/items/874639e361917e5016d421 https:docs.python.org/ja/3/library/2to3.html?highlight=isinstance22 '''23 24 from math import sqrt25 26 if (self.number <= 1) or (isinstance(self.number, float)):27 return False28 29 for i in range(2, int(sqrt(self.number)) + 1):30 if self.number % i == 0:31 return False32 33 return True34 35 def generate(self) -> list:36 '''Generate a list of prime numbers using sieve of Eratosthenes.37 Returns:38 A list of prime numbers that is eqaul to or less than the input39 number.40 Landau notation: O(n log log n)41 See:42 https:beta.atcoder.jp/contests/abc110/submissions/325494743 '''44 45 if self._values:46 return self._values47 48 is_met = [True for _ in range(self.number + 1)]49 is_met[0] = False50 is_met[1] = False51 52 for i in range(2, self.number + 1):53 if is_met[i]:54 self._values.append(i)55 56 for j in range(2 * i, self.number + 1, i):57 is_met[j] = False58 return self._values59 60 61def main():62 from bisect import bisect63 import sys64 65 input = sys.stdin.readline66 67 n = int(input())68 size_max = 10 ** 6 + 1069 p = Prime(size_max)70 ps = p.generate()71 ans = 072 73 for i, qi in enumerate(ps):74 if qi ** 3 > n:75 continue76 77 pi = n (qi ** 3)78 ri = 079 80 if qi - 1 >= pi:81 ri = pi82 else:83 ri = qi - 1 84 85 index = bisect(ps, ri)86 ans += index87 88 print(ans)89 90 91if __name__ == "__main__":92 main()93