Maximum Subsequence CodeForces
来源:互联网 发布:cf东东数据一键刷枪 编辑:程序博客网 时间:2024/05/18 00:53
You are given an array a consisting of n integers, and additionally an integer m. You have to choose some sequence of indices b1, b2, …, bk (1 ≤ b1 < b2 < … < bk ≤ n) in such a way that the value of is maximized. Chosen sequence can be empty.
Print the maximum possible value of .
Input
The first line contains two integers n and m (1 ≤ n ≤ 35, 1 ≤ m ≤ 109).
The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109).
Output
Print the maximum possible value of .
Example
Input
4 4
5 2 4 1
Output
3
Input
3 20
199 41 299
Output
19
Note
In the first example you can choose a sequence b = {1, 2}, so the sum is equal to 7 (and that’s 3 after taking it modulo 4).
In the second example you can choose a sequence b = {3}.
这题的数据范围是35,去枚举可能是不可能的,但是如果n为17或者18,也是可以的,所以这里用状压,把数据先分成两部分,然后把其中一部分用状压枚举所有可能(记得取模),然后排序去重,然后去枚举另一半的组合也是状压去枚举(v1),然后在左边去二分查找值(v2),应该这样去找,使得v1+v2最大,当然最大只能为m-1。
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;int a[262148];int p[40];int findd(int limit,int num)//返回的是最接近num的值,但是不会大于num{ int l=0,r=limit-1; if(a[r]<=num) return a[r]; while(l<=r) { int mid=(l+r)>>1; if(a[mid]==num) return a[mid]; if(a[mid]>num) r=mid-1; else l=mid+1; } return a[l-1];}int main(){ int n,m; int k1=0; int k2=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",p+i),p[i]%=m; int limit=n/2; for(int i=0;i<(1<<limit);i++) { int t=0; for(int j=1;j<=limit;j++) if(i&(1<<(j-1))) t=(t+p[j])%m; a[k1++]=t; } sort(a,a+k1); int num=unique(a,a+k1)-a; int left=n-limit; int ans=0; for(int i=0;i<(1<<left);i++)//枚举 { int temp=0; for(int j=1;j<=left;j++) if(i&(1<<(j-1))) temp=(temp+p[limit+j])%m; int now=m-1-temp; int get=findd(num,now); //cout<<temp<<" "<<get<<endl; ans=max(ans,get+temp); if(ans==m-1)//如果最大就没必要举继续找了 break; } printf("%d\n",ans); return 0;}
- Lexicographically Maximum Subsequence CodeForces
- Maximum Subsequence CodeForces
- codeforces 888E Maximum Subsequence
- Codeforces 124c Lexicographically Maximum Subsequence
- CodeForces 197C Lexicographically Maximum Subsequence
- codeforces 197C Lexicographically Maximum Subsequence
- Codeforces-888E:Maximum Subsequence(思维)
- 【Codeforces Round #172】Codeforces 280D k-Maximum Subsequence Sum
- codeforces Educational Codeforces Round 32 E. Maximum Subsequence
- Codeforces Round #124 (Div. 2) / C. Lexicographically Maximum Subsequence
- [题解]codeforces 280d k-Maximum Subsequence Sum
- codeforces 888 E. Maximum Subsequence(好题)
- codeforces 888E Maximum Subsequence (折半枚举 双向搜索)
- Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- PAT1007. Maximum Subsequence Sum
- PAT_1007: Maximum Subsequence Sum
- UVa:10747 Maximum Subsequence
- hiho 1626 缩写命名 [Offer收割]编程练习赛35 Problem D 二分图匹配
- 思科路由模拟器 -- (5)交换机配置基础、STP协议及VLAN划分
- 输出不同形式九九乘法表
- Thread调用run()和start()方法的区别
- Richard Sutton经典教材《强化学习》第二版公布(附PDF下载)
- Maximum Subsequence CodeForces
- 解决鸡兔同笼问题
- 近年推荐系统论文调查汇总
- HDOJ 1405 The Last Practice
- C语言进阶-第15~16讲:结构体应用(玩转日期和时间)
- while(满足条件后再执行)
- visual studio 2012打开提示 未能将网站×××配置为使用 ASP.NET 4.5 和 尚未在Web服务器上注册,您需要手动将Web服务器配置为使用ASP.NET 4.5,问题解决
- 小白学数据结构——一、线性结构(数组&链表)
- golang基础-浏览器访问服务器、客户端访问服务器(延迟处理)、表单回写并提交、panic处理