ABC248C
stick_to_it · · 个人记录
ABC248C
很简单的一个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;
}
例如上面的状态转移,已经预处理了
要么仅仅初始化
解题思路
-
首先你得看出来是
DP 。 -
设定
f[i][j] 表示前i 个数字,和为j 的方案数。显然初始化有f[i][i] = 1 。 -
状态转移就是枚举第
i 位的状态是啥就好了。
#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;
}