题目链接

题意:给你两个数n,m,和一个大小为n的数组。

让你在数组找一些数使得这些数的和模m最大。

解法:考虑 dfs但是,数据范围不允许纯暴力,那考虑一下折半搜索,一个从头开始往中间搜,一个从后往中间搜。在中间相遇的时间二分更新最大值即可。

#include<bits/stdc++.h>

#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back using namespace std; LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
int n,m,cnt;
int a[37],b[37];
int ans;
int sum1[(1<<20)];
vector<int>v;
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=0;i<n;i++)cin>>a[i],a[i]%=m,ans=max(ans,a[i]);
for(int i=0;i<(1<<(n/2));i++){
for(int j=0;j<n/2;j++)if(i&(1<<j))sum1[i]=(0ll+sum1[i]+a[j])%m;
ans=max(ans,sum1[i]);
v.pb(sum1[i]);
}
sort(v.begin(),v.end());
for(int i=n/2;i<n;i++)b[cnt++]=a[i];
for(int i=0;i<(1<<cnt);i++){
int A=0;
for(int j=0;j<cnt;j++)if((1<<j)&i)A=(0ll+A+b[j])%m;
ans=max(ans,A);
auto pos=upper_bound(v.begin(), v.end(),m-A-1);
pos--;
ans=max(1ll*ans,(0ll+A+(*pos))%m);
}
cout<<ans%m;
return 0;
}

Educational Codeforces Round 32 E. Maximum Subsequence的更多相关文章

  1. Educational Codeforces Round 32

    http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...

  2. Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)

    题目链接:E. Maximum Subsequence 用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很 ...

  3. Educational Codeforces Round 32 Maximum Subsequence CodeForces - 888E (meet-in-the-middle,二分,枚举)

    You are given an array a consisting of n integers, and additionally an integer m. You have to choose ...

  4. Educational Codeforces Round 9 D. Longest Subsequence dp

    D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...

  5. Codeforces Educational Codeforces Round 15 A. Maximum Increase

    A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Educational Codeforces Round 9 D - Longest Subsequence

    D - Longest Subsequence 思路:枚举lcm, 每个lcm的答案只能由他的因子获得,类似素数筛搞一下. #include<bits/stdc++.h> #define ...

  7. Educational Codeforces Round 32 Problem 888C - K-Dominant Character

    1) Link to the problem: http://codeforces.com/contest/888/problem/C 2) Description: You are given a ...

  8. Educational Codeforces Round 17 D. Maximum path DP

    题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...

  9. Educational Codeforces Round 32 Almost Identity Permutations CodeForces - 888D (组合数学)

    A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in thi ...

随机推荐

  1. kubernetes 集群安装etcd集群,带证书

    install etcd 准备证书 https://www.kubernetes.org.cn/3096.html 在master1需要安装CFSSL工具,这将会用来建立 TLS certificat ...

  2. python之常用模块

    python 常用模块 之 (subprocess模块.logging模块.re模块) python 常用模块 之 (序列化模块.XML模块.configparse模块.hashlib模块) pyth ...

  3. mysql 有没有参数都报错“mysql: unknown option”

    报错: [root@XXXX tmp]# mysql -uroot -pmysql: unknown option '--You have new mail in /var/spool/mail/ro ...

  4. Cinder组件

    cinder-api cinder-api 是整个 Cinder 组件的门户,所有 cinder 的请求都首先由 cinder-api 处理. cinder-api 向外界暴露若干 HTTP REST ...

  5. 让linux启动更快的方法

    导读 进行 Linux 内核与固件开发的时候,往往需要多次的重启,会浪费大把的时间. 在所有我拥有或使用过的电脑中,启动最快的那台是 20 世纪 80 年代的电脑.在你把手从电源键移到键盘上的时候,B ...

  6. JVM内存区域详解

    本文分为两部分:一是JVM内存区域的讲解:二是常见的内存溢出异常分析. 1.JVM内存区域 java虚拟机在执行java程序的过程中会把它管理的内存划分为若干个不同的数据区域,这些区域都有各自的用途, ...

  7. kettle基于时间戳增量更新

    思路1: 1.提前建好ts时间表,设置两个字段分别为current_t和load_t,current用于比较原表中日期的上限,load_t则为上次加载的日期,几位原表中日期的下限. create ta ...

  8. 使用mysql将手机号、身份证号等字段进行脱敏

    -- 脱敏姓名 UPDATE wb_person_message SET `name`=(if(LENGTH(name)>6,CONCAT(LEFT(name,1), '**' ),CONCAT ...

  9. 【APIO2016】【UOJ205】【LOJ2568】烟花表演 可合并堆

    题目大意 有一棵树,每条边都有一个边权,现在你要修改边权,使得修改后根到所有叶子的距离相等. 要求所有边权非负. 修改的代价为\(\lvert\)每条边修改前的边权\(-\)修改后的边权\(\rver ...

  10. properties文件操作

    properties文件操作类 可以使用java.util.Properties读取.properties文件中的内容 import java.io.InputStream; import java. ...