您的位置:首页 > 其它

hdu 5464 Clarke and problem(dp)

2015-09-21 13:40 423 查看
[align=left]Problem Description[/align]
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

[align=left]Input[/align]
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).

[align=left]Output[/align]
For each testcase print a integer, the answer.

[align=left]Sample Input[/align]

1

2 3
1 2

[align=left]Sample Output[/align]

2
设dp[i][j]表示到第i个元素余数为j的个数。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define modelo 1000000007
int a[1005];
int dp[1005][1005];
int main()
{
int n,t,q,i,j;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&q);
for (i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]=(a[i]%q+q)%q;
}
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for (i=1;i<=n;i++)
{
for (j=0;j<q;j++)
dp[i][j]=(dp[i-1][j]+dp[i-1][(j+a[i])%q])%modelo;
}
printf("%d\n",dp
[0]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: