DFS生成一下所有排列
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <set>
#include <cmath>
#include <queue>
#include <bitset>
#include <vector>
#include <unordered_map>
#define int long long
#define endl '\n'
#define lowbit(x) x &(-x)
#define mh(x) memset(x, -1, sizeof h)
#define debug(x) cerr << #x << "=" << x << endl;
#define brk exit(0);
using namespace std;
const int N = 55;
const int M = 2 * N;
const int mod = 998244353;
const double esp = 1e-6;
const double pi = acos(-1);
typedef pair<int, int> PII;
typedef long long ll;
struct pair_hash
{
template <class T1, class T2>
size_t operator () (pair<T1, T2> const &pair) const
{
size_t h1 = hash<T1>()(pair.first);
size_t h2 = hash<T2>()(pair.second);
return h1 ^ h2;
}
};
vector<unordered_map<PII, int,pair_hash>> dp(N);
int n;
int a[N], b[N];
int gcd(int a, int b)
{
return b > 0 ? gcd(b, a % b) : a;
}
int lcm(int a,int b)
{
return a / gcd(a, b) * b;
}
int dfs(int x, int gd1, int gd2)
{
if(dp[x][{gd1,gd2}])
return dp[x][{gd1, gd2}];
if(x>n)
return dp[x][{gd1, gd2}] = lcm(gd1, gd2);
int res = 0;
res = max(res,dfs(x + 1, gcd(gd1,a[x]),gcd(gd2,b[x])));
res = max(res,dfs(x + 1, gcd(gd1,b[x]),gcd(gd2,a[x])));
return dp[x][{gd1,gd2}]=res;
}
void solve()
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i] >> b[i];
cout << dfs(1, a[1], b[1])<<endl;
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while (T--)
solve();
return 0;
}