提交时间:2024-07-21 21:16:54

运行 ID: 158120

#include <iostream> #include <vector> #include <numeric> #include <algorithm> bool canRepresentAll(int x, const std::vector<int>& coins) { std::vector<bool> dp(x + 1, false); dp[0] = true; for (const int coin : coins) { for (int i = coin; i <= x; ++i) { if (dp[i - coin]) { dp[i] = true; } } } return dp[x]; } int findMinimalEquivalentSystem(int n, const std::vector<int>& a) { int maxValue = *std::max_element(a.begin(), a.end()) + 1; int minM = n; for (int m = 1; m <= n; ++m) { std::vector<int> b(m); std::iota(b.begin(), b.end(), 1); bool valid = true; for (int value = 1; value < maxValue; ++value) { if (canRepresentAll(value, a) != canRepresentAll(value, b)) { valid = false; break; } } if (valid) { minM = m; break; } } return minM; } int main() { int T; std::cin >> T; while (T--) { int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; ++i) { std::cin >> a[i]; } std::cout << findMinimalEquivalentSystem(n, a) << std::endl; } return 0; }