BestCoder Round #56 1002 hdu 5464 (背包问题)

来源:互联网 发布:mysql indexof 编辑:程序博客网 时间:2024/06/06 13:22

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 a1,a2,...,ana_1, a_2, ..., a_na1,a2,...,an and a number ppp. 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 ofppp(000 is also count as a multiple of ppp). Since the answer is very large, you only need to output the answer modulo 109+710^9+7109+7

Input

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

Output

For each testcase print a integer, the answer.

Sample Input
12 31 2
Sample Output
2Hint:2 choice: choose none and choose all.

题目:给你一组数,从中挑一些数,它们的和是p的倍数,求方案数

分析:因为a[i]可以是负数,所以输入时要处理一下。因为是从数组中选择一些数,所以对于每个数可选可不选,这就成了01背包问题。

d[i][j]表示前i个数,构成余数是j的方法总数。

转态转移方程

d[i][j]=(d[i][j]+d[i-1][j])%MOD

d[i][(j+a[i])%p]=(d[i][(j+a[i])%p]+d[i-1][j])%MOD

#include<iostream>#include<string>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;#define LL long longconst int maxn=1005;const int MOD=1e9+7;int a[maxn],d[maxn][maxn];int main(){    int T,n,p,t;    cin>>T;    while(T--){        scanf("%d%d",&n,&p);        for(int i=1;i<=n;i++){            scanf("%d",&a[i]);            a[i]%=p;            a[i]=(a[i]+p)%p;        }        memset(d,0,sizeof(d));        d[0][0]=1;        for(int i=1;i<=n;i++){            for(int j=0;j<p;j++){                d[i][j]=(d[i][j]+d[i-1][j])%MOD;                d[i][(j+a[i])%p]=(d[i][(j+a[i])%p]+d[i-1][j])%MOD;            }        }       printf("%d\n",d[n][0]);    }}



0 0