Editorial for ICPC NAQ 2016 F - Free Desserts


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
  • Use dynamic programming over the digits of a and b, from left to right, to count the number of solutions.
  • Using a recursive function with memoization (as opposed to an iterative DP), simplifies reconstruction of the pairs of integers we need to output.
  • The solution proceeds in two steps:
    1. Compute the number of solutions to the whole problem and relevant subproblems.
    2. To produce the pairs, repeat the recursion and use the memoization table to avoid processing empty branches.
  • Let's define the subproblem of size k as the original problem restricted to the k least significant digits of P.
  • Let a', b', P' denote the numbers a, b, P restricted to the k least significant digits, including possible leading zeros in a and b.
  • A subproblem of size k also needs the following input:
    • LZa(k), indicating if there is a leading zero in a' at position k,
    • Cy(k), the carry (0 or 1) at position k produced by a'+b',
    • Fa(k), the set of forbidden digits for a', and
    • Fb(k), the set of forbidden digits for b'.
  • Note that Fb(k) is uniquely determined by the other parameters and the digits in P.
  • Let's label each recursion level by its corresponding value k.
  • The solution of a subproblem of size k can be constructed using the solutions of the subproblems of size k-1 as follows:
    • At the k^\text{th} level of recursion, loop through all acceptable digits of a' and b' at position k and through all acceptable values of Cy(k) and LZa(k).
    • Inside the loop construct the corresponding values of Fa(k-1), Fb(k-1), Cy(k-1) and LZa(k-1) and recurse to the (k-1)^\text{th} level providing these values as input to the recursive call.
    • Use the values returned from the (k-1)^\text{th} level and combine them with the acceptable digits at the k^\text{th} level to obtain the solution at the k^\text{th} level. Or, when computing the number of solutions, just sum up the returned values.
  • The complete solution of the original problem is composed of the solutions of two subproblems with parameters:
    • k = length of P,
    • (1) LZa(k) = \texttt{true} or (2) LZa(k) = \texttt{false},
    • Cy(k) = 0,
    • Fa(k) = Fb(k) = the set of digits in P.
  • The memoization table keeps the number of solutions of subproblems for all combinations of k, LZa(k), Cy(k), Fa(k), Fb(k).
  • The size of the table is at most 18 \cdot 2 \cdot 2 \cdot 1024 \cdot 1024 = 75\,497\,472.
  • The complexity is \mathcal O(\log P). However, the big constant factor of \approx 10^6 plays a significant role in any implementation.
  • As there are only 10 digits available, use bitmaps to represent subsets of forbidden digits in a and b.

Comments

There are no comments at the moment.