Files
python/TangDou/LanQiaoBei/LanQiaoBei13ShengSai_ZJ_II/3_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

55 lines
1.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 30;
const int M = 1e4 + 10;
/*
2
3 5
答案:
4
最大不能表示的应该是:3*5-3-5=7
四个不能表示的数是:1 2 4 7
*/
int f[N][M];
int w[N];
int res;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> w[i];
sort(w + 1, w + 1 + n);
int m1 = w[1], m2 = w[2];
int M = m1 * m2 - m1 - m2; // a*b-a-b =7
int g = w[1];
for (int i = 2; i <= n; i++) g = gcd(g, w[i]);
if (g > 1) {
cout << -1 << endl;
return 0;
}
// 完全背包二维写法
for (int i = 1; i <= n; i++)
for (int j = 1; j <= M; j++)
if (j >= w[i])
f[i][j] = max(f[i - 1][j], f[i][j - w[i]] + w[i]);
else
f[i][j] = f[i - 1][j];
for (int i = 1; i <= M; i++)
if (f[n][i] < i) res++;
printf("%d\n", res);
return 0;
}