---
id: 117
lang: en
translation_of: index.md
source_sha256: 574c4ceca092a2d6c42042fa20deb64c493eeecfed1691a89bfebbfe642a4ff8
title: "P1373: Little a and uim's Great Escape — Solution"
description: "This is a fairly typical DP problem for counting the number of ways."
tags: ["OI","DP","Counting DP","Solutions"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "小a (a character in the problem) rendered as “Little a”; uim kept as written."
  - "魔液 and 魔罐 (the problem's terms) rendered as “magic liquid” and “magic jars”."
  - "插值 (literally “interpolation”) is read as a typo for 差值 and rendered as “difference”."
---

## Approach

This is a fairly typical DP problem for counting the number of ways. This is the first time I've done a DP problem on counting the number of ways, so let this solution be the first article in my topic series on counting DP.

When first thinking about the DP equation for this problem, you need to think the way counting DP thinks. The numbers of ways add up, so the operation in each transition is addition rather than taking the max or min. With that, we can really start thinking about the equation for this problem. First, the equation needs the position of the point, and also the states of Little a and uim. How do we bring the magic liquid into it? We add one dimension to record the difference between the two people's magic-liquid jars (of course, when it is negative we can add a k and take it modulo, so that it can be accessed within the positive range). So dp[i][j][h][0/1] can represent the number of ways when we reach (i,j) and the difference between the two people's magic jars is h, where 0 means Little a moves and 1 means uim moves. So the equations are as follows:

F[i][j][h][0] = (F[i][j][h][0] + F[i - 1][j][(h - map[i][j] + k) % k][1]) % P;\
F[i][j][h][0] = (F[i][j][h][0] + F[i][j - 1][(h - map[i][j] + k) % k][1]) % P;\
F[i][j][h][1] = (F[i][j][h][1] + F[i - 1][j][(h + map[i][j]) % k][0]) % P;\
F[i][j][h][1] = (F[i][j][h][1] + F[i][j - 1][(h + map[i][j]) % k][0]) % P;

The code is as follows:

```cpp
// P1373.cpp
#include <iostream>

using namespace std;
// Variables;
const int maxn = 802;
const int INF = 0x7fffffff;
const int P = 1000000007;
int n, m, k;
int map[maxn][maxn];
int F[maxn][maxn][20][2];
// Start to dp;
void dp()
{
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            for (int h = 0; h <= k; h++)
            {
                F[i][j][h][0] = (F[i][j][h][0] + F[i - 1][j][(h - map[i][j] + k) % k][1]) % P;
                F[i][j][h][0] = (F[i][j][h][0] + F[i][j - 1][(h - map[i][j] + k) % k][1]) % P;
                F[i][j][h][1] = (F[i][j][h][1] + F[i - 1][j][(h + map[i][j]) % k][0]) % P;
                F[i][j][h][1] = (F[i][j][h][1] + F[i][j - 1][(h + map[i][j]) % k][0]) % P;
            }
}

int main()
{
    // Input;
    cin >> n >> m >> k, ++k;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            // Initialize;
            cin >> map[i][j], F[i][j][map[i][j]][0] = 1;
    dp();
    long long ans = 0;
    // Calculate the answer;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            ans += F[i][j][0][1], ans %= P;
    // Output;
    cout << ans;
    return 0;
}
```
