dp hdu 5464 Clarke and problem

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(1T10) - the number of test cases. 
T test cases follow. 
The first line contains two positive integers n,p(1n,p1000) 
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.

~开心,自己根据写出来的,还特别简洁。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <algorithm>
 7 #include <vector>
 8 #define ll __int64
 9 #define mod 1000000007
10 using namespace std;
11 int dp[1005][1005],nums[1005];
12 int main(void)
13 {
14     int t;
15     cin>>t;
16     while(t--)
17     {
18         int n,p;
19         scanf("%d %d",&n,&p);
20         for(int i = 1; i <= n; i++)
21         {
22             scanf("%d",&nums[i]);
23             nums[i] %= p;
24             if(nums[i] < 0)
25                 nums[i] += p;
26         }
27 
28         memset(dp,0,sizeof(dp));
29         dp[0][0] = 1;
30         for(int i = 1; i <= n; i++)
31         {
32             for(int j = 0; j <= p; j++)
33             {
34                 dp[i][j] = dp[i-1][j] + dp[i-1][(j-nums[i]+p)%p];
35                 dp[i][j] %= mod;
36             }
37         }
38         printf("%d
",dp[n][0]);
39     }
40     return 0;
41 }
原文地址:https://www.cnblogs.com/henserlinda/p/4827335.html