Educational Codeforces Round 32 E. Maximum Subsequence
题目链接
题意:给你两个数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的更多相关文章
- Educational Codeforces Round 32
http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...
- Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)
题目链接:E. Maximum Subsequence 用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很 ...
- 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 ...
- Educational Codeforces Round 9 D. Longest Subsequence dp
D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...
- 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 ...
- Educational Codeforces Round 9 D - Longest Subsequence
D - Longest Subsequence 思路:枚举lcm, 每个lcm的答案只能由他的因子获得,类似素数筛搞一下. #include<bits/stdc++.h> #define ...
- 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 ...
- Educational Codeforces Round 17 D. Maximum path DP
题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...
- 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 ...
随机推荐
- JavaWeb工程中web.xml基本配置(转载学习)
一.理论准备 先说下我记得xml规则,必须有且只有一个根节点,大小写敏感,标签不嵌套,必须配对. web.xml是不是必须的呢?不是的,只要你不用到里面的配置信息就好了,不过在大型web工程下使用该文 ...
- 一个方法教你认识ref(简单易懂)
参数分为值类型和引用类型,当我们将一个值类型的参数进行传递到另一个方法的时候相当于,将这个变量进行复制到该方法进行操作,但是不会对该变量原始的值有影响. 但是有时候我们需要他有“影响”于是ref就出现 ...
- RPC框架原理简述:从实现一个简易RPCFramework说起(转)
摘要: 本文阐述了RPC框架与远程调用的产生背景,介绍了RPC的基本概念和使用背景,之后手动实现了简易的RPC框架并佐以实例进行演示,以便让各位看官对RPC有一个感性.清晰和完整的认识,最后讨论了RP ...
- ABP拦截器之UnitOfWorkRegistrar(一)
ABP中UnitOfWorkRegistrar拦截器是整个ABP中非常关键的一个部分,这个部分在整个业务系统中也是用的最多的一个部分,这篇文章的主要思路并不是写如何使用ABP中的UnitOfWork, ...
- 如何使用Excel表格状态栏动态查看统计
该文是以前的博文,本文中有一列是快递单号,其实这一列根本不需要,一般快递几天就到,在excel表中存快递单号,纯属浪费时间,快递单号一般都会有客户留存联,而且也登入了网上的表格,所以个人用的excel ...
- docker基础维护命令
docker images显示当前存在的images docker ps,显示当前的container docker rm containerId 删除指定的container(需要先停止,才能删除) ...
- jap篇 之 JSTL标签库
JSTL标签库: JSTL: JSP Standard Tag Library 作用:和[EL配合]使用,可以让用户[尽可能少的使用java源码]. 1,导入jar包 导入(复制粘贴到项目中的lib目 ...
- min-max容斥/最值反演及其推广
设\(S\)是一个集合,\(\max(S)\)和\(\min(S)\)分别表示集合中的最大值与最小值. 那么有如下式子成立: \[\max(S)=\sum_{T \subseteq S}(-1)^{| ...
- Zookeeper 启蒙
2018-12-14 关键词: Zookeeper入门介绍 . Zookeeper是什么.Zookeeper架构解析.Zookeeper应用场景.Zookeeper有什么用 本篇文章系笔者依据当前所掌 ...
- FileZilla-02
WordPress的权限方案 通常,所有文件应由您的Web服务器上的用户(ftp)帐户拥有,并且应该可由该帐户写入.在共享主机上,文件永远不应归Web服务器进程本身所有(有时这是www,或apache ...