正解:$meet\ in\ the\ middle$

解题报告:

传送门$QwQ$.

发现数据范围为$n\leq 35$,所以$2^{\frac{n}{2}}$是可做的.

所以先拆成$A,B$两个集合分别跑个爆搜,然后分别排个序,对于$A$中的每个数$A_i$,发现有两种可能是最优解.一种是$A_i+B_i<M$.一种是$M<A_i+B_i<2M$(显然会先给$A,B$中的数取膜嘛$QwQ$.

然后对于第一种用个指针扫下就行,第二种发现这时选的一定是$B_{max}$

然后就做完了$QwQ$.

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define gc getchar()
#define ri register int
#define rb register bool
#define rc register char
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i)
#define e(i,x) for(ri i=head[x];i;i=edge[i].nxt) const int N=;
int n,mod,a[N],as;
vector<int>V[]; il int read()
{
rc ch=gc;ri x=;rb y=;
while(ch!='-' && (ch>'' || ch<''))ch=gc;
if(ch=='-')ch=gc,y=;
while(ch>='' && ch<='')x=(x<<)+(x<<)+(ch^''),ch=gc;
return y?x:-x;
}
void dfs(ri sum,ri nw,ri lim,ri num)
{
if(sum>=mod)sum-=mod;
if(nw>lim){V[num].push_back(sum);return;}
dfs(sum+a[nw],nw+,lim,num);dfs(sum,nw+,lim,num);
} int main()
{
//freopen("888E.in","r",stdin);freopen("888E.out","w",stdout);
n=read();mod=read();rp(i,,n)a[i]=read()%mod;dfs(,,n>>,);dfs(,(n>>)+,n,);
sort(V[].begin(),V[].end());sort(V[].begin(),V[].end());
unique(V[].begin(),V[].end());unique(V[].begin(),V[].end());
ri sz=V[].size(),r=V[].size()-,mx=V[][r];
rp(i,,sz-)
{while(~r && V[][i]+V[][r]>=mod)--r;if(~r)as=max(as,V[][i]+V[][r]);;as=max(as,(V[][i]+mx)%mod);}
printf("%d\n",as);
return ;
}

讲道理我真的好喜欢meet-in-the-middle啊,,,贼好写QwQ

随机推荐

  1. LeetCode70 Climbing Stairs

    题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  2. SPOJ 8073 The area of the union of circles (圆并入门)

    Sphere Online Judge (SPOJ) - Problem CIRU [求圆并的若干种算法,圆并扩展算法]_AekdyCoin的空间_百度空间 参考AekdyCoin的圆并算法解释,根据 ...

  3. IDEA使用中文api鼠标提示的设置

    最近都在用IDEA来练习,发现有的方面确实比eclipse好用,eclipse里面可添加中文的API 提示,对初期的我帮助很大,但是IDEA却没有找到添加的地方,一直以来还以为不支持这个功能,比较遗憾 ...

  4. 初识block

    我们可以把Block当做Objective-C的匿名函数.Block允许开发者在两个对象之间将任意的语句当做数据进行传递,往往这要比引用定义在别处的函数直观.另外,block的实现具有封闭性(clos ...

  5. OpenStack组件系列☞Keystone

    Keystone(OpenStack Identity Service)是 OpenStack 框架中负责管理身份验证.服务规则和服务令牌功能的模块.用户访问资源需要验证用户的身份与权限,服务执行操作 ...

  6. python selenium处理iframe和弹框(一)

    处理iframe和弹框 # encoding:utf-8 from selenium import webdriver import time driver = webdriver.Firefox() ...

  7. centos下iptables使用

    iptables的使用规则: 1.表    (table) 包含4个表(哪个表是当前表取决于内核配置选项和当前模块):4个表的优先级由高到低:raw-->mangle-->nat--> ...

  8. 上传图片保存到MySql数据库并显示--经验证有效

    以下方法仅供参考,只是介绍下这一种方法而已.欢迎指正!! 前台(image.html):  1<html> 2<head> 3<title>上传图片</tit ...

  9. Spring < context:annotation-config> 、< context:component-scan>、< mvc:annotation-driven />注解配置

    Spring 中在使用注解(Annotation)会涉及到< context:annotation-config> 和 < context:component-scan>配置, ...

  10. [转载] linux find 命令

    转载自 http://www.jb51.net/os/RedHat/1307.html Linux下find命令在目录结构中搜索文件,并执行指定的操作. Linux下find命令提供了相当多的查找条件 ...