很简单的一个DP
重点是规定一些问题。
- 有些状态已经初始化了就不需要再次更新了。
#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;
}例如上面的状态转移,已经预处理了 ,但是在累加的状态转移的时候再次更新了 这会使得部分 会被更新到大于 这肯定是不对的。
要么仅仅初始化 之后更新的时候 会从 更新过来,要么全都初始化,但是不更新 的状态。
解题思路
-
首先你得看出来是 。
-
设定 表示前 个数字,和为 的方案数。显然初始化有 。
-
状态转移就是枚举第 位的状态是啥就好了。
#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;
}#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;
}