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 ...
随机推荐
- R语言学习——数组
> #数组(array)与矩阵类似,但维度可大于2.可通过array函数构建,形式如下:myarray<-array(vector,dimensions,dimnames)> #其中 ...
- 强大的scrollReveal库,炫酷的页面缓入效果。
首先我问来看一下这个强大的插件能做出什么效果,下面是我找的一个网站: http://kepler.gl/#/, 接下来看看官网给出的效果:https://scrollrevealjs.org/. 是不 ...
- Django-CRM项目学习(一)-admin组件
开始今日份整理 1.admin组件使用 1.1 创建django项目以及开启APP01 略 1.2 创建类 使用django自带的sqlite3的小型文件型的数据库 注:使用sqlite3类型的数据库 ...
- yidiandian
hzwer libreoj (需要拿新版的打开)
- 使用 xUnit 编写 ASP.NET Core 单元测试
还记得 .NET Framework 的 ASP.NET WebForm 吗?那个年代如果要在 Web 层做单元测试简直就是灾难啊..NET Core 吸取教训,在设计上考虑到了可测试性,就连 ASP ...
- 用JS解决url地址中参数乱码的问题
var url = window.location.herf;//获取url地址 var obj = {}; //最后输出的对象 var reg = /\?/; //要匹配的正则表达式 if(url. ...
- 关于mysql中的count()函数
1.count()函数是用来统计表中记录的一个函数,返回匹配条件的行数. 2.count()语法: (1)count(*)---包括所有列,返回表中的记录数,相当于统计表的行数,在统计结果的时候,不会 ...
- openstack搭建之-基础服务配置(7)
基础环境准备,所需服务器及说明 172.16.2.51 base.test.com 基础服务节点 172.16.2.52 ctrl.test.com 控制节点 172.16.2.53 ...
- Java JPA @Transient 在Hibernate中应用
jpa @Transient - 走过程序员的路 - CSDN博客https://blog.csdn.net/lafengwnagzi/article/details/55511066 Hiberna ...
- DEVOPS 运维开发系列
DEVOPS 运维开发系列四:ITIL事态管理流程.事态监控系统设计以及基于Devops的效率提升实践 - watermelonbig的专栏 - CSDN博客https://blog.csdn.net ...