---
id: 1677
lang: en
translation_of: index.md
source_sha256: 0f5f8dd5a105ab8f807872208c4983f99a1d60a18cdbb1b4ddc2da50a0358de8
title: "[Codeforces 1204E] Natasha, Sasha and the Prefix Sums — Solution"
description: "A DP over the numbers of 1s and -1s, plus a second 2D DP that counts the sequences contributing nothing, gives the answer."
tags: ["OI","DP","Counting","Counting DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## Main Idea and Derivation

This problem is a good one.

We can just do a brute-force DP: let the state $dp[i][j]$ be the answer for $i$ copies of $1$ and $j$ copies of $-1$. Then we can transition from the case with one fewer $1$ and the case with one fewer $-1$:

- For the case with one fewer $1$, that is, $dp[i - 1][j]$, we put the $1$ at the front of such sequences, which adds one to the maximum prefix sum of every sequence, so the contribution is $dp[i - 1][j] + {i + j - 1 \choose j}$.
- For the case with one fewer $-1$, that is, $dp[i][j - 1]$, we put the $-1$ at the front of such intervals, and two cases arise: for sequences whose contribution is greater than $0$, the contribution of the $-1$ is the number of such sequences; if the contribution was already less than or equal to $0$, then there is no contribution. So we also need to separately count the number of sequences with no contribution and add it back.

<!--more-->

Now we only need to sort out the number of sequences with no contribution. We find that counting this number actually also requires knowing the numbers of $1, -1$, so we need to set up another 2D DP, with a state much like the previous one:

$$
zero[i][j] = zero[i - 1][j] + zero[i][j - 1]
$$

In particular, when $i>j$, the value is $0$; when $i = 0$, the value is $1$. So this problem is done. Finally, here is the derived formula:

$$
dp[i][j] = dp[i - 1][j] + {i + j - 1 \choose j} + dp[i][j - 1] - ({i + j - 1 \choose i} - zero[i][j - 1])
$$

## Code

```cpp
// CF1204E.cpp
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int MAX_N = 4020, mod = 998244853;

int fac[MAX_N], fac_inv[MAX_N], n, m, k[MAX_N][MAX_N], dp[MAX_N][MAX_N];

int quick_pow(int bas, int tim)
{
    int ans = 1;
    while (tim)
    {
        if (tim & 1)
            ans = 1LL * ans * bas % mod;
        bas = 1LL * bas * bas % mod;
        tim >>= 1;
    }
    return ans;
}

int combinator(int n_, int k_) { return 1LL * fac[n_] * fac_inv[n_ - k_] % mod * fac_inv[k_] % mod; }

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++)
        k[0][i] = 1;
    for (int i = 1; i <= n; i++)
        for (int j = i; j <= m; j++)
            k[i][j] = (1LL * k[i - 1][j] + 1LL * k[i][j - 1]) % mod;

    for (int i = fac[0] = 1; i < MAX_N; i++)
        fac[i] = 1LL * fac[i - 1] * i % mod;
    fac_inv[MAX_N - 1] = quick_pow(fac[MAX_N - 1], mod - 2);
    for (int i = MAX_N - 2; i >= 1; i--)
        fac_inv[i] = 1LL * fac_inv[i + 1] * (i + 1) % mod;
    fac_inv[0] = 1;

    for (int i = 1; i <= n; i++)
        dp[i][0] = i;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            ll tmp = 1LL * combinator(i + j - 1, j) + 1LL * dp[i - 1][j] + 1LL * dp[i][j - 1] - (1LL * combinator(i + j - 1, i) - k[i][j - 1]);
            while (tmp < 0)
                tmp += mod;
            tmp %= mod;
            dp[i][j] = tmp;
        }
    printf("%d", dp[n][m]);
    return 0;
}
```
