Problem solution · Python

ABC358 E — Alphabet Tiles

ABC358 E — Alphabet Tiles: a Python solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Dynamic programming
Source
KATO-Hiro AtCoder Solutions
Length
97 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

Dynamic programming

For ABC358 E — Alphabet Tiles, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 97 lines of Python from the credited upstream file abc358_e.py.
  • The implementation visibly relies on sequence storage, ordered lookup, cached states.
  • No explicit loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

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 codeABC358 E — Alphabet Tiles · 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     k = int(input())    c = list(map(int, input().split()))    dp = [0] * (k + 1)    dp[0] = 1    size = 10**3    mod = 998244353    ans = 0     combination = Combination(max_value=10**3 + 10, mod=mod)     for ci in c:        ndp = [0] * (k + 1)         for j in range(size + 1):            for add in range(ci + 1):                nj = j + add                 if nj > k:                    break                 ndp[nj] += dp[j] * combination.count_nCr(nj, add)                ndp[nj] %= mod         dp = ndp     ans = sum(dp[1:]) % 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 ↗