Home AtCoder Beginner Contest 265 (E) DP
Post
Cancel

AtCoder Beginner Contest 265 (E) DP

E. Warp (DP)

定义 $dp[n][i][j]$: the number of paths that end up in $(x,y)$ after $n$ teleportations

复杂度: $O(N^3(\log{N} + \log{M}))$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    vector<long long> dx(3), dy(3);
    for (int i = 0; i < 3; i++)
    {
        cin >> dx[i] >> dy[i];
    }

    set<pair<long long, long long>> obstacles;
    for (int i = 0; i < m; i++)
    {
        long long x, y;
        cin >> x >> y;

        obstacles.insert({x, y});
    }

    long long mod = 998244353;

    // dp[n][i][j]: the number of paths that end up in (x,y) after n teleportations
    map<pair<long long, long long>, long long> dp;
    dp[{0, 0}] = 1;

    for (int i = 0; i < n; i++)
    {
        map<pair<long long, long long>, long long> newdp;
        for (auto & p : dp)
        {
            long long x = p.first.first, y = p.first.second;
            long long val = p.second;

            for (int j = 0; j < 3; j++)
            {
                long long nx = x + dx[j], ny = y + dy[j];

                if (!obstacles.count({nx, ny}))
                {
                    newdp[{nx, ny}] += val;
                    newdp[{nx, ny}] %= mod;
                }
            }
        }

        swap(dp, newdp);
    }

    long long ans = 0;
    for (auto & p : dp)
    {
        ans += p.second;
        ans %= mod;
    }

    cout << ans << endl;

    return 0;
}
  • 方法2

定义 $dp[i][j][k]$: the number of paths in which you make each of the moves $x$, $y$, and $z$ times, during the $n$ teleportations

复杂度: $O(N^3\log{M})$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    vector<long long> dx(3), dy(3);
    for (int i = 0; i < 3; i++)
    {
        cin >> dx[i] >> dy[i];
    }

    set<pair<long long, long long>> obstacles;
    for (int i = 0; i < m; i++)
    {
        long long x, y;
        cin >> x >> y;

        obstacles.insert({x, y});
    }

    long long mod = 998244353;

    // dp[i][j][k]: the number of paths in which you make each of the moves x, y, and z times, during the n teleportations
    vector<vector<long long>> dp(1, vector<long long>(1));
    dp[0][0] = 1;

    for (int i = 0; i < n; i++)
    {
        vector<vector<long long>> newdp(i + 2, vector<long long>(i + 2));
        
        for (long long a = 0; a <= i; a++)
        {
            for (long long b = 0; b <= i; b++)
            {
                long long x = a * dx[0] + b * dx[1] + (i - a - b) * dx[2];
                long long y = a * dy[0] + b * dy[1] + (i - a - b) * dy[2];

                for (long long k = 0; k < 3; k++)
                {
                    if (!obstacles.count({x + dx[k], y + dy[k]}))
                    {
                        newdp[a + (k == 0)][b + (k == 1)] += dp[a][b];
                        newdp[a + (k == 0)][b + (k == 1)] %= mod;
                    }
                }
            }
        }

        swap(dp, newdp);
    }

    long long ans = 0;
    for (int a = 0; a <= n; a++)
    {
        for (int b = 0; b <= n; b++)
        {
            ans += dp[a][b];
            ans %= mod;
        }
    }
    
    cout << ans << endl;

    return 0;
}
This post is licensed under CC BY 4.0 by the author.

珂朵莉树

快速幂