Bestcoders 56 Clarke and problem

Clarke and problem

 
 Accepts: 169
 
 Submissions: 372
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
Problem Description

Clarke is a patient with multiple personality disorder. One day, Clarke turned into a student and read a book. Suddenly, a difficult problem appears:
You are given a sequence of number a_1, a_2, ..., a_na1,a2,...,an and a number pp. Count the number of the way to choose some of number(choose none of them is also a solution) from the sequence that sum of the numbers is a multiple of pp(00 is also count as a multiple of pp). Since the answer is very large, you only need to output the answer modulo 10^9+7109+7

Input

The first line contains one integer T(1 \le T \le 10)T(1T10) - the number of test cases.
TT test cases follow.
The first line contains two positive integers n, p(1 \le n, p \le 1000)n,p(1n,p1000)
The second line contains nn integers a_1, a_2, ... a_n(|a_i| \le 10^9a1,a2,...an(ai109).

Output

For each testcase print a integer, the answer.

Sample Input
1
2 3
1 2
Sample Output
2

Hint:
2 choice: choose none and choose all.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
#define N 1000 + 10
#define LL long long
#define INF 0x3f3f3f3f
const int mod = 1000000000 + 7;

int n, p;
int a[N], d[N][N];

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &p);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);

        memset(d, 0, sizeof d);
        d[0][0] = 1;
        for(int i = 1; i <= n; i++)
            for(int j = 0; j < p; j++)
        {
            int tmp = ((LL)(j - a[i]) % p + p) % p;
            d[i][j] = ((LL)d[i - 1][tmp] + d[i - 1][j]) % mod;
        }

//        for(int i = 0; i <= n; i++)
//        {
//            for(int j = 0; j < p; j++)
//                cout << d[i][j] << " ";
//            cout << endl;
//        }
        printf("%d\n", d[n][0]);
    }
    return 0;
}

/*

1
2 3
1 2


*/


你可能感兴趣的:(ACM)