题目链接

题意:给你两个数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. .net 添加api不能访问的问题

    在一个.netmvc项目中,本身没有提供api后来想添加api就会出现问题.会发生添加的apicontrol不能访问的情况.这种情况一般是因为,global文件中,application_start( ...

  2. 迄今为止 .Net 平台功能最强大,性能最佳的 JSON 序列化和反序列化库。

    Swifter.Json 这是迄今为止 .Net 平台功能最强大,性能最佳的 JSON 序列化和反序列化库. Github : https://github.com/Dogwei/Swifter.Js ...

  3. linux下开启、关闭、重启mysql服务命令

    一. 启动1.使用 service 启动:service mysql start2.使用 mysqld 脚本启动:/etc/inint.d/mysql start3.使用 safe_mysqld 启动 ...

  4. php的接口,抽象类,trait(转载,合并)

    一. 抽象类abstract class 1 .抽象类是指在 class 前加了 abstract 关键字且存在抽象方法(在类方法 function 关键字前加了 abstract 关键字)的类. 2 ...

  5. 解决SVN一直弹出登录问题,eclipse.tmatesoft.com

    Windows->preferences->Igonored Resources Add Pattern.. .project .classpath .settings 添加这三个: 把C ...

  6. springboot 打war

    pom.xml <packaging>war</packaging> <!-- 打包设置 --> <plugins> <plugin> &l ...

  7. MySQL-基本命令

    一.登录命令 mysql -r 用户名 -p 密码 二.创建用户 create user '用户名'@'主机名' identified by '密码' #主机名:指定该用户在哪个主机上可以登陆,如果是 ...

  8. python 装饰器练习题

    1.写出完整的装饰器(不用开了带参装饰器,就是普通装饰器)语法 2.有一个计算两个数和的方法,为其添加一个确保两个参数都是int或float类型的装饰器,保证运算不会抛异常 3.有一个一次性录入人名并 ...

  9. codeforces660C

    Hard Process CodeForces - 660C You are given an array a with n elements. Each element of a is either ...

  10. bzoj3277-串

    Code #include<cstdio> #include<iostream> #include<cmath> #include<cstring> # ...