#include <stdio.h>
int n;
long long int m,max_mod=0;
int a[15];
void dfs(int pos,long long int current_sum)
{
long long current_mod=current_sum%m;
if (current_mod > max_mod)
{
max_mod = current_mod;
}
if(pos==n)
{
return ;
}
dfs(pos+1,current_sum);//跳过pos
dfs(pos+1,current_sum+a[pos]);//不跳过pos
}
int main()
{
scanf("%d %lld",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
dfs(0,0);
printf("%lld",max_mod);
return 0;
}
调用DFS,两个方程dfs(pos+1,current_sum);//跳过pos和 dfs(pos+1,current_sum+a[pos]);//不跳过pos。

京公网安备 11010502036488号