跳到正文

个人记录

ABC248C

stick_to_it约 7 分钟
原文
文章信息与版本

文章信息

发布
2023年4月15日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 7 分钟

版本

  1. v1当前

ABC248C

很简单的一个DP

重点是规定一些问题。

  • 有些状态已经初始化了就不需要再次更新了。
C++
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>

#define endl '\n'

using namespace std;

typedef long long LL;

const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const long long LNF = 0x3f3f3f3f3f3f3f3f;

void IOS() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const int N = 60, M = N * N;

int n, m, k;
LL f[N][M];

void solve() {
    cin >> n >> m >> k;
    for (int i = 0; i <= n; i ++ ) f[i][i] = 1;
    for (int i = 1; i <= n; i ++ ) {
        for (int j = i; j <= k; j ++ ) {
            for (int c = 1; c <= m; c ++ ) {
                if (j >= c) f[i][j] = (f[i][j] + f[i - 1][j - c]) % mod;
            }
        }
    }
    LL res = 0;
    for (int j = n; j <= k; j ++ ) {
        res = (res + f[n][j]) % mod;
    }
    cout << res << endl;
}

int main() {
    IOS();
    int T = 1;
    // cin >> T;
    while (T -- ) {
        solve();
    }
    return 0;
}

例如上面的状态转移,已经预处理了 f[i][i]f[i][i] ,但是在累加的状态转移的时候再次更新了 f[i][i]f[i][i] 这会使得部分 f[i][i]f[i][i] 会被更新到大于 11 这肯定是不对的。

要么仅仅初始化 f[0][0]f[0][0] 之后更新的时候 f[i][i]f[i][i] 会从 f[i−1][i−1]f[i - 1][i - 1] 更新过来,要么全都初始化,但是不更新 f[i][i]f[i][i] 的状态。

解题思路

  1. 首先你得看出来是 DPDP 。

  2. 设定 f[i][j]f[i][j] 表示前 ii 个数字,和为 jj 的方案数。显然初始化有 f[i][i]=1f[i][i] = 1 。

  3. 状态转移就是枚举第 ii 位的状态是啥就好了。

C++
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>

#define endl '\n'

using namespace std;

typedef long long LL;

const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const long long LNF = 0x3f3f3f3f3f3f3f3f;

void IOS() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const int N = 60, M = N * N;

int n, m, k;
LL f[N][M];

void solve() {
    cin >> n >> m >> k;
    for (int i = 0; i <= n; i ++ ) f[i][i] = 1;
    for (int i = 1; i <= n; i ++ ) {
        for (int j = i + 1; j <= k; j ++ ) {
            for (int c = 1; c <= m; c ++ ) {
                if (j >= c) f[i][j] = (f[i][j] + f[i - 1][j - c]) % mod;
            }
        }
    }
    LL res = 0;
    for (int j = n; j <= k; j ++ ) {
        res = (res + f[n][j]) % mod;
    }
    cout << res << endl;
}

int main() {
    IOS();
    int T = 1;
    // cin >> T;
    while (T -- ) {
        solve();
    }
    return 0;
}
C++
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>

#define endl '\n'

using namespace std;

typedef long long LL;

const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const long long LNF = 0x3f3f3f3f3f3f3f3f;

void IOS() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const int N = 60, M = N * N;

int n, m, k;
LL f[N][M];

void solve() {
    cin >> n >> m >> k;
    f[0][0] = 1;
    for (int i = 1; i <= n; i ++ ) {
        for (int j = i; j <= k; j ++ ) {
            for (int c = 1; c <= m; c ++ ) {
                if (j >= c) f[i][j] = (f[i][j] + f[i - 1][j - c]) % mod;
            }
        }
    }
    LL res = 0;
    for (int j = n; j <= k; j ++ ) {
        res = (res + f[n][j]) % mod;
    }
    cout << res << endl;
}

int main() {
    IOS();
    int T = 1;
    // cin >> T;
    while (T -- ) {
        solve();
    }
    return 0;
}
原文