正解:$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. oracle函数 localtimestamp

    [功能]:返回会话中的日期和时间 [参数]:没有参数,没有括号 [返回]:日期 [示例]select localtimestamp from dual; 返回:14-11月-08 12.35.37.4 ...

  2. etcd 在超大规模数据场景下的性能优化

    作者 | 阿里云智能事业部高级开发工程师 陈星宇(宇慕) 概述 etcd是一个开源的分布式的kv存储系统, 最近刚被cncf列为沙箱孵化项目.etcd的应用场景很广,很多地方都用到了它,例如kuber ...

  3. loadrunner_关联检查点参数化

    1.保存变量函数 lr_save_string("192.168.xx.xx","ip"); URL=http://{ip} 2.检查点函数 web_reg_f ...

  4. poj 2442 Sequence (Priority Queue)

    2442 -- Sequence 真郁闷,明明方法是对的,为什么我的代码老是那么的慢._(:з」∠)_ 这题要想考虑两列的情况,然后逐列拓展. 代码如下: #include <cstdio> ...

  5. supersockets服务器配置热更新

    Keywords: 配置,热更新 此功能能够允许你在不重启服务器的前提下更新服务器实例的配置. (仅限1.6.5及其以上版本)

  6. HTML静态网页---标签

    一. 创建HTML: (一) body的属性: bgcolor 页面背景色 background   背景壁纸.图片 text   文字颜色 topmargin   上边距 leftmargin    ...

  7. halcon坐标转换(机器人坐标转换用)

    #图像坐标r:=[431, 355, 507, 53, 507]c:=[505, 543, 316, 127, 883]#物理坐标(例如机器人坐标)r1:=[0, 2.0, -2.0, 10, -2. ...

  8. win10 uwp xaml 绑定接口

    本文告诉大家如何在 xaml 绑定属性使用显式继承接口 早上快乐 就在你的心问了我一个问题,他使用的属性是显式继承,但是无法在xaml绑定 我写了简单的代码,一个接口和属性 public class ...

  9. squid+iptables实现网关防火墙

    需求说明:此服务器用作网关.MAIL(开启web.smtp.pop3).FTP.DHCP服务器,内部一台机器(192.168.0.254)对外提供dns服务,为了不让无意者轻易看出此服务器开启了ssh ...

  10. display常用的三种值:block、inline、inline-block

    display:block(块级元素 ) 会独占一行,多个block元素会各自新起一行.默认情况下,block元素宽度自动填满其父级宽度 display:inline(内联(行级)元素 ) 不会独占一 ...