提交时间:2026-06-17 06:45:56

运行 ID: 91819

#include <iostream> #include <vector> #include <algorithm> #include <unordered_set> using namespace std; vector<int> all; // 所有卡片 unordered_set<int> s; // 快速判重 int st[105]; // 卡片使用状态 // 当前状态:last上一个数,返回当前玩家是否必胜 bool dfs(int last) { // 枚举所有可选的数 for (int i = 0; i < all.size(); ++i) { if (st[i]) continue; int v = all[i]; // 必须是约数或倍数 if (last != -1 && v % last != 0 && last % v != 0) continue; st[i] = 1; bool win = !dfs(v); st[i] = 0; if (win) return true; } // 无棋可走,败 return false; } int main() { // 读取第一行:所有卡片 int x; while (cin.peek() != '\n' && cin >> x) { all.push_back(x); } // 读取第二行:可选起点 vector<int> can; while (cin >> x) { can.push_back(x); } vector<int> res; // 枚举每个起点 for (int v : can) { // 找到第一个未使用的v int pos = -1; for (int i = 0; i < all.size(); ++i) { if (!st[i] && all[i] == v) { pos = i; break; } } if (pos == -1) continue; st[pos] = 1; bool ok = dfs(v); st[pos] = 0; if (ok) res.push_back(v); } if (res.empty()) { cout << -1 << endl; } else { sort(res.begin(), res.end()); cout << res[0] << endl; } return 0; }