---
id: 108
lang: en
translation_of: index.md
source_sha256: 6aa44fd1933ebaf4f4ddfafb4e4e7c31bca46e5f4e22cf0fc2d0f4af346a3048
title: "P1052: River Crossing — Solution"
description: "The data is huge, so a conventional path DP array won't fit; applying a modulo to the distances compresses the path, and then a normal DP works."
tags: ["OI","DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "跳不到的总会挑不到: 挑 is a typo for 跳 (jump) in the original; translated as the intended “will never be reached”."
  - "过河 (NOIP 2005) rendered as “River Crossing”."
---

## Approach

The data in this problem is huge, and with the conventional path DP the array won't fit. However, we can apply a modulo to the distances: after taking the modulo, as the frog jumps, the spots it should reach will always be reached, and the spots it can't reach will never be reached (compare with the basics of game theory). Now that we've compressed the path, we can do a normal DP.

For the other details (there are way too many details), see the code.

## Code

```cpp
// P1052.cpp
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 400000;
const int maxm = 1000;
const int INF = 0x7fffffff;
// positions of stones;
int stone[maxm];
// used for DP;
int F[maxn];
// the distance between 2 stones;
int diff[maxn];
// flag[pos] to indicate whether there is a stone at pos;
bool flag[maxn];
int L, S, T, M;

void DP()
{
    // start loop to the farthest;
    for (int i = 1; i <= L + T; i++)
        for (int k = S; k <= T; k++)
        {
            // to transform from F[i-k];
            // k is the step number;
            if (i - k >= 0)
                F[i] = min(F[i], F[i - k]);
            // if there is a stone, count it!
            F[i] += (flag[i]) ? 1 : 0;
        }
}

int main()
{
    // input;
    cin >> L >> S >> T >> M;
    for (int i = 1; i <= M; i++)
        cin >> stone[i];
    sort(stone + 1, stone + M + 1);
    diff[0] = 0;
    // to calc and zip with the distances;
    for (int i = 1; i <= M; i++)
        diff[i] = (stone[i] - stone[i - 1]) % 2520;
    // to update the shortened distances and the flag[];
    for (int i = 1; i <= M; i++)
        stone[i] = stone[i - 1] + diff[i], flag[stone[i]] = true;
    L = stone[M];
    // to init for the F;
    for (int i = 0; i <= L + T; i++)
        F[i] = M;
    F[0] = 0;
    // Start to DP();
    DP();
    // to figure out the ans;
    int ans = M;
    for (int i = L; i < L + T; i++)
        ans = min(ans, F[i]);
    // output;
    cout << ans;
    return 0;
}
```
