CodeForces - 888E(思维,折半)
程序员文章站
2024-03-20 18:02:46
...
题目
分两半二进制枚举。
枚举第一部分。
第二部分每次枚举完去第一部分查找。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define N 40
#define LL long long
LL a[N];
set<LL>s;
int main()
{
int n, n1, n2;
LL m;
scanf("%d%lld", &n, &m);
for (int i = 0; i < n; i++)
{
scanf("%lld", &a[i]);
a[i] %= m;
}
sort(a, a + n);
n1 = n / 2, n2 = n - n1;
LL sum, ans = 0;
for (int i = 0; i < (1 << n1); i++)
{
sum = 0;
for (int j = 0; j < n1; j++)
{
if ((1 << j) & i)sum += a[j];
sum %= m;
}
s.insert(sum);
}
set<LL>::iterator it;
for (int i = 0; i < (1 << n2); i++)
{
sum = 0;
for (int j = 0; j < n2; j++)
{
if ((1 << j) & i)sum += a[j + n1];
sum %= m;
}
it = s.upper_bound(m - sum - 1); it--;
ans = max(ans, sum + *it);
}
cout << ans << endl;
return 0;
}
上一篇: Session
下一篇: JavaScript实现常见查找算法
推荐阅读
-
Codeforces 888E Maximum Subsequence(折半搜索)
-
CodeForces - 888E(思维,折半)
-
[CodeForces 888E] Maximum Subsequence
-
C. Balanced Bitstring(字符串+思维) Codeforces Round #668 (Div. 2)
-
【 Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair】思维题
-
Codeforces Round #459 (Div. 2) C. The Monster(枚举+思维)
-
Codeforces Round #449 (Div. 2) B. Chtholly's request (思维
-
CodeForces - 1118F1 Tree Cutting (Easy Version) (树形dp/dfs+思维)
-
CodeForces 959 E Mahmoud and Ehab and the xor-MST(异或 思维)
-
Codeforces Round #553 (Div. 2) B. Dima and a Bad XOR(异或+思维)