uva 11026

来源:互联网 发布:诺基亚n9价格淘宝 编辑:程序博客网 时间:2024/05/01 06:56

    A Grouping Problem
Input: Standard Input

Output: Standard Output

 

You are given a set of N integers.  You can take K different elements from them to make a group. Two groups will bedifferent if there is at least one element which is not common to both. Forexample, if there are 4 elements – a, b,c, d and you are asked to take two elements then ab, ad, bc,cd are allvalid and different groups. A grouping system is complete if for a particular K, number of different groups is themaximum. In the former case, {ab, bc, cd, bd,ad, ac} is a complete grouping system.

 

For a particular completegrouping system, the fitness iscalculated in the following way –

  1. Each group of a grouping system contributes a part – the multiplication of all numbers of that group
  2. Contribution from all groups are added
  3. The fitness is equivalent to Total Contribution mod M, M is the bounding parameter

In our example, for K = 2, thefitness is F2 = (ab+bc+cd+bd+ad+ac) mod M. If K= 1, then fitness is F1 = (a+b+c+d) mod M.

 

Here, in this problem you have tofind the complete grouping system with maximum fitness.

 

Input

Each test case starts with twopositive integer N (2<=N<=1000) and M (1<=M<231). Innext few lines there will be N positive integers. Each integer will be at best1000. Input will be terminated by a case where N=M=0.

 

Output

For each test case, print in aline the maximum fitness possible for a grouping system.

 

SampleInput                             Outputfor Sample Input


4 10
1 2 3 4
4 100
1 2 3 4
4 6
1 2 3 4
0 0
 

5

50

5

 


Problemsetter: Md.Kamruzzaman, EPS

典型的动态规划题目,注意数据范围,代码如下:

#include<iostream>
#define maxn 1001
using namespace std;
long long p[maxn][maxn];
int main()
{
    long long n,m,num[maxn];
    while(cin>>n>>m&&(n!=0||m!=0))
    {
        long long i,j,max=-1;
        for(i=1;i<=n;i++)
        {
            cin>>num[i];
            num[i]%=m;
        }
        p[1][0]=1;
        p[1][1]=num[1];
        p[1][2]=0;
        for(i=2;i<=n;i++)
        {
            p[i][0]=1;
            for(j=1;j<=i;j++)
            {
                p[i][j]=(p[i-1][j-1]*num[i]+p[i-1][j])%m;
            }
            p[i][j+1]=0;
        }
        for(i=1;i<=n;i++)
        {
            if(p[n][i]>max)
                max=p[n][i];
        }
        cout<<max<<endl;
    }
    return 0;
}