跳到正文

个人记录

2026.9.17 dp模拟赛

Millie128_80066B约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年9月17日
收录
2026年9月17日
更新
2026年11月14日
篇幅
约 5 分钟

版本

  1. v3当前
  2. v2
  3. v1

比赛

AT_abc159_f [ABC159F] Knapsack for All Segments

题意

给定一个长度为 NN 的整数序列 A1,A2,…,ANA_1, A_2, \ldots, A_N 和一个正整数 SS

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

算法分析

赛时代码(伪?)
C++
#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
AC code
C++
#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

题意

nn 个城市,编号为 1,2,…,n1,2,\dots,n,由 mm 条双向道路连接

道路的数量最多比城市的数量多 1010,但任意两个城市之间仍然可以通过一条或多条道路互相到达

不能走回边

从城市 11 出发,恰好经过 kk 步到达城市 nn 的旅行计划有多少种

算法分析

  1. n−1≤m≤n+10n-1 \le m \le n + 10 所以最多只有 1111 条非树边
  2. 非树边构成的环对答案一定是有影响的,所以我们考虑优化图
    • 标记 11 和 nn 及非树边上对应的端点并保存
    • edge node edge:对于度为 22 的点,窝们考虑合并收缩为一条边,长度为 a+ba + b
    • node edge node:对于度为 11 的点,直接删除(不能走回边)
  3. 因此,最大点数为 11∗2+2(1和n)=2411 * 2 + 2(1 和 n) = 24
    • dpi,jdp_{i, j}:走 ii 步最后一步沿有向边 j 到达 toito_i 的方案数
    • dpL,nextedge=(dpL,nextedge+dpi,edge)dp_{L, nextedge} = (dp_{L, nextedge} + dp_{i, edge})
    • 对于 j=(i,toi)j =(i, to_i),若 ii 与 toito_i 在树上的唯一路径的边数为 i−1i - 1,则 dpi,j=1dp_{i, j} = 1 否则 dpi,j=0dp_{i, j} = 0
    • ans=∑dpk,jans = \sum dp_{k, j}
原文