题面:


赛时 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;
}
十分感谢!