2026.9.17 dp模拟赛
Millie128_80066B · · 个人记录
比赛
AT_abc159_f [ABC159F] Knapsack for All Segments
题意
给定一个长度为
所有满足
算法分析
::::success[赛时代码(伪?)]
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 4e3 + 10;
const ll mod = 998244353;
ll dp[N][N];
int a[N];
int main() {
freopen("bag.in", "r", stdin);
freopen("bag.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, s;
cin >> n >> s;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i <= n; i++) dp[i][0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= s; j++) {
dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod;
if (j - a[i] >= 0) dp[i][j] = (dp[i][j] + dp[i - 1][j - a[i]]) % mod;
}
ll ans = 0;
for (int i = 1; i <= n; i++) ans = (ans + dp[i][s]) % mod;
cout << ans;
return 0;
}
// 3B6PAC
::::
::::success[AC code]
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 3e3 + 7;
const int mod = 998244353;
int n, m, k;
int a[maxn], s, dp[maxn];
signed main()
{
cin >> n >> s;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
if (s > a[i])
{
ans = (ans + dp[s - a[i]] * (n - i + 1)) % mod;
}
else if (s == a[i])
{
ans = (ans + i * (n - i + 1)) % mod;
}
for (int j = s; j >= a[i]; j--)
{
dp[j] = (dp[j] + dp[j - a[i]]) % mod;
}
dp[a[i]] = (dp[a[i]] + i) % mod;
}
cout << ans << '\n';
return 0;
}
::::
P16359 [BalticOI 2026] Tourist's Journey
题意
- `node edge node`:对于度为 $1$ 的点,直接删除(不能走回边)
- 因此,最大点数为
11 * 2 + 2(1 和 n) = 24 -
-
-
dp_{L, nextedge} = (dp_{L, nextedge} + dp_{i, edge}) - 对于
j =(i, to_i) ,若i 与to_i 在树上的唯一路径的边数为i - 1 ,则dp_{i, j} = 1 否则dp_{i, j} = 0 -
ans = \sum dp_{k, j}
-