Table of Contents

ExplanationImplementation

Official Analysis (C++)

Explanation

Let aia_i be Bessie's ith instruction and bjb_j be Elsie's jth instruction. Additionally, let I(i,j)I(i, j) denote the set of unique expressions we can make by interleaving Bessie's first ii instructions and Elsie's first jj instructions. For example, in the sample:

3
12+
+02
  • I(0,0)I(0, 0): {0}\{0\} (interleaving [ ][\ ] and [ ][\ ])
  • I(0,1)I(0, 1): {+y}\{+y\} (interleaving [ ][\ ] and [+y][+y])
  • I(2,1)I(2, 1): {+y,+2y}\{+y, +2y\} (interleaving [1,2][*1, *2] and [+y][+y])
  • I(3,3)I(3, 3): {0,+x,+2x}\{0, +x, +2x\} (interleaving [1,2,+x][*1, *2, +x] and [+y,0,2][+y, *0, *2])

Notation

In the below solution,

  • S|S| denotes the number of elements in a set SS
  • STS \cup T denotes the union of sets SS and TT (elements in either SS or TT)
  • STS \cap T denotes the intersection of sets SS and TT (elements in both SS and TT)
  • E(i,j)E(i, j) denotes an arbitrary expression in I(i,j)I(i, j)
  • set of expressionsinstruction\text{set of expressions} \rightarrow \text{instruction} denotes applying that instruction to each expression in the set

So, how do we calculate I(i,j)|I(i, j)|?

When either ii or jj are 0, either Bessie or Elsie's instruction set will be empty, meaning the interleaving is uniquely determined and thus I(i,0)=I(0,i)=1\boxed{|I(i, 0)| = |I(0, i)| = 1} for all iNi \leq N.

Great, but what if ii and jj are both nonzero? Evidently, any interleaving of Bessie's first ii instructions and Elsie's first jj instructions must either end with instruction aia_i or instruction bjb_j. We can thus rewrite the expressions that end with aia_i as I(i1,j)aiI(i - 1, j) \rightarrow a_i (let's denote this set as AA) and the ones that end with bjb_j as I(i,j1)bjI(i, j - 1) \rightarrow b_j (let's denote this set as BB). We also have the following:

  1. As long as aia_i isn't 0*0, A=I(i1,j)\boxed{|A| = |I(i - 1, j)|}
  2. As long as bjb_j isn't 0*0, B=I(i,j1)\boxed{|B| = |I(i, j - 1)|}
  3. I(i,j)=ABI(i, j) = A \cup B

Take some time to convince yourself of these, especially points 1 and 2!

Not Convinced?

Use the fact that if we apply the same, non-0*0 operation to two distinct expressions, the results will still be distinct.

At this point, you may be tempted to conclude that

I(i,j)=AB=A+B=I(i1,j)+I(i,j1)|I(i, j)| = |A \cup B| = |A| + |B| = |I(i - 1, j)| + |I(i, j - 1)|

Don't make this mistake! Remember the Principle of Inclusion-Exclusion:

ST=S+TST|S \cup T| = |S| + |T| - |S \cap T|

For example, if S={1,2}S = \{1, 2\} and T={2,3}T = \{2, 3\}, we overcount 22 if we simply concatenate these two sets, so we need to subtract ST|S \cap T|, which, in this case, is {2}=1|\{2\}| = 1.

In our case, we overcount AB|A \cap B|. So which expressions are in this set? This is a crucial part of the solution, so make sure to spend some time thinking about it before reading the answer below! To make this easier, you may want to first assume neither Bessie nor Elsie has the 0*0 instruction, as it's not too hard to figure out whether I(i,j)I(i, j) contains the 00 expression and reduces the number of edge cases we need to consider.

Hint

Detailed Explanation

TL;DR

Implementation

Time Complexity: O(N2)\mathcal{O}(N^2)

C++

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e3 + 1;
const int MOD = 1e9 + 7;
// adding and subtracing under mod
ll ad(ll &a, ll b) { return a = (a + b) % MOD; }

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!