You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.2 KiB
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
int num[105];
vector<int> ac[105];
int dfs(int x) {
for (int i = ac[x].size() - 1; i >= 0; i--)
if (num[ac[x][i]]) {
num[ac[x][i]]--;
int res = dfs(ac[x][i]);
num[ac[x][i]]++;
if (res == -1) return 1;
}
return -1;
}
int main() {
for (;;) {
int x;
scanf("%d", &x);
num[x]++;
if (getchar() == '\n') break;
}
vector<int> can;
for (;;) {
int x;
if (scanf("%d", &x) == -1)break;
can.push_back(x);
if (getchar() == '\n') break;
}
sort(can.begin(), can.end());
for (int i = 1; i <= 100; i++)
if (num[i]) {
num[i]--;
for (int j = 1; j <= 100; j++)
if (num[j] && (i % j == 0 || j % i == 0))
ac[i].push_back(j);
num[i]++;
}
for (int i = 0; i < can.size(); i++) {
num[can[i]]--;
if (dfs(can[i]) == -1)return printf("%d", can[i]), 0;
num[can[i]]++;
}
puts("-1");
return 0;
}