2026.9.17 dp模拟赛

· · 个人记录

比赛

AT_abc159_f [ABC159F] Knapsack for All Segments

题意

给定一个长度为 N 的整数序列 A_1, A_2, \ldots, A_N 和一个正整数 S

所有满足 1 \leq L \leq R \leq N 的整数对 (L, R) 的 f(L, R) 之和

算法分析

::::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

题意

道路的数量最多比城市的数量多 $10$,但任意两个城市之间仍然可以通过一条或多条道路互相到达 不能走回边 从城市 $1$ 出发,恰好经过 $k$ 步到达城市 $n$ 的旅行计划有多少种 ### 算法分析 1. $n-1 \le m \le n + 10$ 所以最多只有 $11$ 条非树边 2. 非树边构成的环对答案一定是有影响的,所以我们考虑优化图 - 标记 $1$ 和 $n$ 及非树边上对应的端点并保存 - `edge node edge`:对于度为 $2$ 的点,窝们考虑合并收缩为一条边,长度为 $a + b
- `node edge node`:对于度为 $1$ 的点,直接删除(不能走回边)
  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}