AT_abc159_f [ABC159F] Knapsack for All Segments
题意
给定一个长度为 的整数序列 和一个正整数
所有满足 的整数对 的 之和
算法分析
赛时代码(伪?)
#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
#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
题意
个城市,编号为 ,由 条双向道路连接
道路的数量最多比城市的数量多 ,但任意两个城市之间仍然可以通过一条或多条道路互相到达
不能走回边
从城市 出发,恰好经过 步到达城市 的旅行计划有多少种
算法分析
- 所以最多只有 条非树边
- 非树边构成的环对答案一定是有影响的,所以我们考虑优化图
- 标记 和 及非树边上对应的端点并保存
edge node edge:对于度为 的点,窝们考虑合并收缩为一条边,长度为node edge node:对于度为 的点,直接删除(不能走回边)
- 因此,最大点数为
-
- :走 步最后一步沿有向边 j 到达 的方案数
- 对于 ,若 与 在树上的唯一路径的边数为 ,则 否则