站外题求助
  • 板块题目总版
  • 楼主multiple_set
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/4/8 17:29
  • 上次更新2024/4/8 20:12:02
查看原帖
站外题求助
1216364
multiple_set楼主2024/4/8 17:29

题面:

赛时 meet-in-the-middle 写挂了(悲

有没有巨佬来帮我调一下:

#include <bits/stdc++.h>
using namespace std;
int n, m, mx, tot, a[50], x[2000010];
//void dfs(int dep, int cnt)
//{
//	if (dep == n + 1) return mx = max(mx, cnt), void();
//	dfs(dep + 1, (cnt + a[dep]) % m), dfs(dep + 1, cnt);
//}
void dfs1(int dep, int cnt)
{
	if (dep == n / 2 + 1) { x[++tot] = cnt; return ; }
	dfs1(dep + 1, (cnt + a[dep]) % m), dfs1(dep + 1, cnt);
}
void dfs2(int dep, int cnt)
{
	if (dep == n + 1)
	{
		int pos = lower_bound(x + 1, x + tot + 1, m - cnt) - x - 1;
		if (pos >= 1 && pos <= n) mx = max(mx, cnt + x[pos]);
		return ;
	}
	dfs2(dep + 1, (cnt + a[dep]) % m), dfs2(dep + 1, cnt);
}

int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i] %= m;
//	if (n <= 20) dfs(1, 0), printf("%d\n", mx);
//	else
//	{
		dfs1(1, 0), sort(x + 1, x + tot + 1);
		dfs2(n / 2 + 1, 0), printf("%d\n", mx);
//	}
	return 0;
}

十分感谢!

2024/4/8 17:29
加载中...