提交时间:2026-06-17 06:44:52
运行 ID: 91817
#include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <algorithm> using namespace std; // 记忆化:当前剩余卡片 + 上一个数 -> 是否必胜 unordered_map<string, bool> memo; // 把状态编码成字符串用于记忆化 string encode(const vector<int>& cnt, int last) { string s; for (int x : cnt) s += to_string(x) + ","; s += ":" + to_string(last); return s; } // 博弈核心:当前计数cnt,上一个数last,当前玩家是否必胜 bool dfs(vector<int> cnt, int last) { string key = encode(cnt, last); if (memo.count(key)) return memo[key]; // 枚举所有能选的数 for (int v = 1; v <= 100; ++v) { if (cnt[v] == 0) continue; if (last != -1 && v % last != 0 && last % v != 0) continue; // 选v cnt[v]--; bool win = !dfs(cnt, v); cnt[v]++; // 回溯 if (win) return memo[key] = true; } // 无棋可走,败 return memo[key] = false; } int main() { vector<int> all, can; int x; // 读第一行:所有卡片 while (cin.peek() != '\n' && cin >> x) all.push_back(x); cin.ignore(); // 读第二行:可选起点 while (cin >> x) can.push_back(x); // 计数数组 cnt[v] = 数字v的张数 vector<int> cnt(101, 0); for (int v : all) cnt[v]++; vector<int> res; // 枚举每个起点 for (int s : can) { if (cnt[s] == 0) continue; auto 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; }