Problem solution · Python

ABC250 D — 250-like Number

ABC250 D — 250-like Number: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Direct simulation
Source
KATO-Hiro AtCoder Solutions
Length
93 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For ABC250 D — 250-like Number, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. 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.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC250 D — 250-like Number · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  class Prime:    '''Represents a snippet for prime numbers.    '''     def __init__(self, number):        self.number = number        self._values = []     def is_included(self) -> bool:        '''Determine whether it is a prime number.        Args:            number: Int of number (greater than 0).        Returns:            True if the input number was prime.            False if the input number was not prime.        See:            https://qiita.com/srtk86/items/874639e361917e5016d4            https://docs.python.org/ja/3/library/2to3.html?highlight=isinstance#2to3fixer-isinstance        '''         from math import sqrt         if (self.number <= 1) or (isinstance(self.number, float)):            return False         for i in range(2, int(sqrt(self.number)) + 1):            if self.number % i == 0:                return False         return True     def generate(self) -> list:        '''Generate a list of prime numbers using sieve of Eratosthenes.        Returns:            A list of prime numbers that is eqaul to or less than the input            number.        Landau notation: O(n log log n)        See:            https://beta.atcoder.jp/contests/abc110/submissions/3254947        '''         if self._values:            return self._values         is_met = [True for _ in range(self.number + 1)]        is_met[0] = False        is_met[1] = False         for i in range(2, self.number + 1):            if is_met[i]:                self._values.append(i)                 for j in range(2 * i, self.number + 1, i):                    is_met[j] = False        return self._values  def main():    from bisect import bisect    import sys     input = sys.stdin.readline     n = int(input())    size_max = 10 ** 6 + 10    p = Prime(size_max)    ps = p.generate()    ans = 0     for i, qi in enumerate(ps):        if qi ** 3 > n:            continue         pi = n // (qi ** 3)        ri = 0         if qi - 1 >= pi:            ri = pi        else:            ri = qi - 1                    index = bisect(ps, ri)        ans += index     print(ans)  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗