C - LCM of GCDs(记忆化DFS)

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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值