代码厌氧
#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int n,m,a[35],mid,ans;
vector<int>b,c;
void dfs1(int i,int res){
if(i>mid){
b.push_back(res);
return;
}
dfs1(i+1,(res+a[i])%m);
dfs1(i+1,res);
}
void dfs2(int i,int res){
if(i>n){
c.push_back(res);
return;
}
dfs2(i+1,(res+a[i])%m);
dfs2(i+1,res);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
mid=n>>1;
dfs1(1,0);
dfs2(mid+1,0);
sort(b.begin(),b.end());
sort(c.begin(),c.end());
int lb=b.size(),lc=c.size();
for(int i=0;i<lb;i++){
int pos=lower_bound(c.begin(),c.end(),m-b[i])-c.begin()-1;
ans=max(ans,b[i]+c[pos]);
}
ans=max(ans,b[lb-1]+c[lc-1]-m);
cout<<ans<<"\n";
return 0;
}
输入
35 10
86 66 98 91 61 71 14 58 49 92 13 97 13 22 98 83 85 29 85 41 51 16 76 17 75 25 71 10 87 11 9 34 3 6 4
正确输出
9