Credit: AtCoder
C2321 - Coins
Define $dp[i][j]$ to be the probability after tossing the first $i$ coins, and receiving $j$ heads.
Then, our probability of flipping $j$ heads from the first $i$ coins is the addition of the following:
- Flipping a head with probability $p[i-1]$, which contributes $dp[i-1][j-1]\times p[i-1]$.
- Flipping a tail with probability $(1-p[i-1])$, which contributes $dp[i-1][j]\times (1-p[i-1])$.
$$dp[i][j] = dp[i-1][j-1]\times p[i-1]+dp[i-1][j]\times(1-p[i-1])$$
C2322 - Numbers and Stones
Define $dp[i]$ as if it's possible to win with $i$ stones remaining. Then keep two loops, one for values of
$i$ $(1\le i\le K)$, and the other for each value in $A$. If $i\ge j$ and it was not possible to win a game
with $i-a_j$ stones, then - because the turns alternate - a game with $i$ stones is winnable.
Hence, the DP transition formula is:
$$dp[i] = (min_{1\le j\le n}(dp[i-a_j])) \oplus 1$$
Note that $\oplus$ is the symbol for XOR.
C2323 - Not Bipartite Matching
Define $dp[i][S]$: (S is a bitmask) number of matchings if first $i$ people took the jobs at the set bits of $S$.
Example: $dp[2][01001_2]$ means for first 2 people they take the first and fourth jobs in whatever order.
Easy to see that there are only $2^N$ occupied dp spaces, so compress the first dimension.
($i = popcount(S)$)
Transition:
$$dp[bitmask \text{ OR } (1\ll i)] = (dp[bitmask \text{ OR } (1\ll i)]+dp[bitmask])\mod {10^9+7}$$
C2324 - Knapsack but TLE
Note that this problem is the same as the classic Knapsack DP problem, but instead of a high value
of $v_i$, there is a high value of $w_i$.
Define $dp[i]$ as the minimum weight we can achieve for value $i$.
Hence the DP transition is:
$$dp[j+v[i]]=min(dp[j+v[i]], dp[j]+w[i])$$
Simply find the maximum $i$ such that $dp[i]\le W$.
C2325 - Yep Grid
First, note that the number of ways to walk from cell $(i,j)$ to $(x,y)$ is $x-i+y-j\choose x-i$,
which can be calculated in $O(1)$ with precomputation of factorials and modular inverse of factorials.
Let $dp[i]$ be the number of ways to walk from $(1,1)$ to the $i$th obstacle such that obstacle $i$
is the first obstacle passed in the path.
To calculate this, note that if there are no obstacles from $(1,1)$ to $(r_i, c_i)$, then there are
a total of $r_i-1+c_i-1\choose r_i-1$ ways.
We can subtract this value from the number of ways to pass through a obstacles in the path from $(1, 1)$
to $(r_i, c_i)$. This can be done by looping through all obstacles $j$ such that $r_j\le r_i$ and $c_j\le c_i$.
To calculate the answer, we can simply add an extra obstacle at $(H, W)$ and take the resulting $dp$
value.
$$dp[i]={r_i-1+c_i+1\choose r_i-1}-\sum_{j=0}^i \left(((r_j\le r_i)\land (c_j\le c_i))\times dp[j]\times
{r_i-r_j+c_i-c_j\choose r_i-r_j}\right)$$
Note that you have to sort the obstacles in some order such that $A[i]$ cannot reach $A[j]$ for all $j \lt i$.