Problem solution · Python

ABC405 E — Fruit Lineup

ABC405 E — Fruit Lineup: 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
91 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 ABC405 E — Fruit Lineup, 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

  • 91 lines of Python from the credited upstream file abc405_e.py.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • 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 codeABC405 E — Fruit Lineup · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  class Combination:    """Count the total number of combinations.    nCr % mod.    nHr % mod = (n + r - 1)Cr % mod.     Args:        max_value: Max size of list. The default is 500,050        mod      : Modulo. The default is 10 ** 9 + 7.     Landau notation: O(n)     See:    http://drken1215.hatenablog.com/entry/2018/06/08/210000    """     def __init__(self, max_value=500050, mod=10**9 + 7):        self.max_value = max_value        self.mod = mod        self.fac = [0 for _ in range(self.max_value)]        self.finv = [0 for _ in range(self.max_value)]        self.inv = [0 for _ in range(self.max_value)]         self.fac[0] = 1        self.fac[1] = 1        self.finv[0] = 1        self.finv[1] = 1        self.inv[1] = 1         for i in range(2, self.max_value):            self.fac[i] = self.fac[i - 1] * i % self.mod            self.inv[i] = self.mod - self.inv[self.mod % i] * (self.mod // i) % self.mod            self.finv[i] = self.finv[i - 1] * self.inv[i] % self.mod     def count_nCr(self, n, r):        """Count the total number of combinations.            nCr % mod.            nHr % mod = (n + r - 1)Cr % mod.         Args:            n   : Elements. Int of number (greater than 1).            r   : The number of r-th combinations. Int of number                  (greater than 0).         Returns:            The total number of combinations.         Landau notation: O(1)        """         if n < r:            return 0        if n < 0 or r < 0:            return 0         return self.fac[n] * (self.finv[r] * self.finv[n - r] % self.mod) % self.mod  def main():    import sys     input = sys.stdin.readline     a, b, c, d = map(int, input().split())    mod = 998244353    comb = Combination(max_value=4 * 10**6 + 10, mod=mod)    ans = 0     # リンゴとオレンジを先に並べる    # 最も右側のオレンジの右側にブドウを置く    # 最も右側のリンゴの右側にバナナを置く     # 最も右側のリンゴを境界として、左右に分ける    # 左側: リンゴa個、オレンジb'個、右端をリンゴで固定    # 右側: オレンジb - b'個、バナナc個、ブドウd個     # b'を全探索    for b1 in range(b + 1):        count1 = comb.count_nCr(a + b1 - 1, b1)        count2 = comb.count_nCr(b - b1 + c + d, c)        ans += count1 * count2        ans %= mod     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 ↗