提交时间:2026-06-17 06:45:27
运行 ID: 91818
#include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; map<pair<vector<int>, int>, bool> dp; bool dfs(vector<int> cnt, int last) { pair<vector<int>, int> sta = {cnt, last}; if (dp.count(sta)) return dp[sta]; bool can_win = false; for (int v = 1; v <= 100; ++v) { if (cnt[v] == 0) continue; if (last != -1) { if (v % last != 0 && last % v != 0) continue; } vector<int> nc = cnt; nc[v]--; if (!dfs(nc, v)) { can_win = true; break; } } return dp[sta] = can_win; } int main() { vector<int> all, opts; int x; while (cin >> x) all.push_back(x); cin.clear(); cin.ignore(); string line; getline(cin, line); vector<int> can; size_t pos = 0; while (pos < line.size()) { while (pos < line.size() && isspace(line[pos])) pos++; if (pos >= line.size()) break; int num = 0; while (pos < line.size() && isdigit(line[pos])) { num = num * 10 + line[pos] - '0'; pos++; } can.push_back(num); } vector<int> cnt(101, 0); for (int v : all) cnt[v]++; vector<int> res; for (int s : can) { if (cnt[s] == 0) continue; vector<int> tmp = cnt; tmp[s]--; if (dfs(tmp, s)) { res.push_back(s); } } if (res.empty()) { cout << -1 << endl; } else { sort(res.begin(), res.end()); cout << res[0] << endl; } return 0; }