题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5464
解题思路:
由于p很小,而ai很大,所以先把ai%p,由于ai可能有负数,所以ai=(ai%p+p)%p
接下来就是在[0,p-1]这个范围内去找数了。
dp[i][j]表示前i个数中,取得的数是p的倍数的方案数。状态转移就很简单了,直接上代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1005;
const int mod = 1e9+7;
int n,p;
int a[maxn],dp[maxn][maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%d%d",&n,&p);
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
a[i] = (a[i] % p + p) % p;
}
dp[0][0] = 1;
for(int i = 1; i <= n; i++)
for(int j = 0; j < p; j++)
dp[i][j] = (dp[i-1][j] + dp[i-1][(p+j-a[i])%p]) % mod;
printf("%d\n",dp[n][0]);
}
return 0;
}