原题传送门

E. Maximum Subsequence
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 .

Examples
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}.

分析:看到n≤35的条件时确实应该想到什么——假如n≤25,那么直接穷举2n情况即可,那么这个想法能否应用到35的情况中呢?答案时肯定的:使用Meet-in-the-Middle算法,即将原数组拆成两半,每一半都枚举2n/2个和(当然要mod m了),然后分别排序,枚举左半边取的和,再在右半边二分查找能拼出来的最大的和(假如左半边为x,则对右半边数组lower_bound(c,c+(1<<nr),m-x)-c-1,最后减1的原因是恰好求出小于m-x的最大值,即为答案)。这个算法能够成立的原因是:左右两边是加的关系,因此不存在加起来大于m,并且mod m后比不大于m的情况大——即(x+y)mod m<=max(x,y) mod m (0≤x,y<m)。

实现起来还是很简单的。

代码

Codeforces 888E Maximum Subsequence的更多相关文章

  1. Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))

    888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...

  2. codeforces 880E. Maximum Subsequence(折半搜索+双指针)

    E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. CF 888E Maximum Subsequence——折半搜索

    题目:http://codeforces.com/contest/888/problem/E 一看就是折半搜索?……然后排序双指针. 两个<m的数加起来如果>=m,一定不会更新答案.因为- ...

  4. CF 888E Maximum Subsequence

    一道比较套路的题,看到数据范围就差不多有想法了吧. 题目大意:给一个数列和\(m\),在数列任选若干个数,使得他们的和对\(m\)取模后最大 取膜最大,好像不能DP/贪心/玄学乱搞啊.\(n\le35 ...

  5. 888E - Maximum Subsequence 中途相遇法

    Code: #include<cstdio> #include<algorithm> #include<cstring> #include<string> ...

  6. [CodeForces - 197C] C - Lexicographically Maximum Subsequence

    C - Lexicographically Maximum Subsequence You've got string s, consisting of only lowercase English ...

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

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

  8. Codeforces Round #648 (Div. 2) E. Maximum Subsequence Value 贪心

    题意:E.Maximum Subsequence Value 题意: 给你n 个元素,你挑选k个元素,那么这个 k 集合的值为 ∑2i,其中,若集合内至少有 max(1,k−2)个数二进制下第 i 位 ...

  9. 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

随机推荐

  1. spark-submit参数说明--on YARN

    示例: spark-submit [--option value] <application jar> [application arguments] 参数名称 含义 --master M ...

  2. 让intellij idea 14 支持ES6语法

    用eclipse做前端开发,用到了webpack,结果各种依赖导致软件卡的一比,简直不能动!虽然在同事的帮忙下,修改了一下配置,但仍然卡的没脾气.改用intellij idea 14解决了卡的问题,但 ...

  3. S7-200和S7-300profibus-DP通信

    一.S7-200CN的cup可以通过EM277接入DP网络 二.CPU315-2DP做主站,S7-200CUP做从站 三. 通信题目 四.硬件组态 1.主站的DP组态,地址为2 2.EM277作为从站 ...

  4. Hybris 项目工程配置

    1.控制台页面进入platform目录 cd F:\hybris640\hybris\bin\platform 并运行 setantenv.bat 生成对应的ant. 2.运行 ant moduleg ...

  5. [Count the numbers satisfying (m + sum(m) + sum(sum(m))) equals to N]

    Given an integer N, the task is to find out the count of numbers M that satisfy the condition M + su ...

  6. Java Error : type parameters of <T>T cannot be determined during Maven Install

    遇到了一个问题如下: Caused by the combination of generics and autoboxing. 这是由于泛型和自动装箱联合使用引起的. 可以查看以下两个回答:   1 ...

  7. 移动端的一些常用meta标签

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...

  8. 移动端IOS第三方输入法遮挡底部Input及android键盘回落留白问题

    var interval; //消息框获取焦点 $('#J_text').focus(function(){ interval = setInterval(function() { scrollToE ...

  9. CSS3 自定义动画(animation)

    除了在之前的文章中介绍过的 CSS3 的变形 (transformation) 和转换 (transition) 外,CSS3 还有一种自由度更大的自定义动画,开发者甚至可以使用变形(transfor ...

  10. 房上的猫:StringBuffer类

    一.使用StringBuffer类 StringBuffer类位于java.lang包中,是String类的增强类 步骤:  1.声明StringBuffer对象并初始化 StringBuffer s ...