您的位置:首页 > 其它

HDU-5464-Clarke and problem

2016-07-25 22:16 120 查看
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 a1,a2,…,an and a number p. 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 p(0 is also count as a multiple of p). Since the answer is very large, you only need to output the answer modulo 109+7

Input

The first line contains one integer T(1≤T≤10) - the number of test cases.

T test cases follow.

The first line contains two positive integers n,p(1≤n,p≤1000)

The second line contains n integers a1,a2,…an(|ai|≤109).

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.

题解:用dp[i][j]存储前i个数的任意组合对p取模为j的方法数

#include<iostream>
using namespace std;
#define Mod 1000000007
int dp[1005][1005];
int main()
{
int a[1005];
int T, n, p;
cin >> T;
while (T--)
{
cin >> n >> p;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
a[i] = (a[i] % p + p) % p;
}
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
int x;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < p; j++)
{
x = (j + a[i]) % p;
dp[i][x] = (dp[i - 1][j] + dp[i - 1][x]) % Mod;
}
}
cout << dp
[0] << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM dp