跳到正文

题解

题解:P14360 [CSP-J 2025] 多边形 / polygon(民间数据)

Jason227约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月21日
篇幅
约 2 分钟
题目
P14360

版本

  1. v1当前

题解:P14360 [CSP-J 2025] 多边形 / polygon

题目传送门

题意:

从 nn 根木棍中选出若干根拼成多边形,需要满足两个条件:

  • 至少选 33 根
  • 总长度大于最长木棍长度的两倍

思路:

  1. 将木棍按长度从小到大排序
  2. 对于每根木棍作为最大值的情况,计算前 i−1i-1 根木棍中长度和大于当前木棍的方案数
  3. 使用动态规划背包统计各种长度和的方案数
  4. 通过补集思想计算合法方案

代码

C++
#include <bits/stdc++.h>
#define Jason227 return
#define code 0
using namespace std;

const int mod = 998244353;
const int N = 5005;

int n, a[N];
long long f[N];

long long p2(int k) {
    long long r = 1;
    for (int i = 0; i < k; i++) r = r * 2 % mod;
    return r;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + n + 1);
    f[0] = 1;
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        if (i >= 3) {
            long long tot = p2(i - 1);
            long long bad = 0;
            for (int j = 0; j <= a[i]; j++) bad = (bad + f[j]) % mod;
            ans = (ans + tot - bad + mod) % mod;
        }
        for (int j = N - 1; j >= a[i]; j--) {
            f[j] = (f[j] + f[j - a[i]]) % mod;
        }
    }
    cout << ans << endl;
    Jason227 code;
}

时间复杂度:O(n×max⁡(ai))O(n \times \max(a_i)),可以通过本题。

AC 记录

原文