Clarke and problem
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,...,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
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 ).
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.
Source
/************************************************************************/
附上该题对应的中文题
Clarke and problem
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
问题描述
克拉克是一名人格分裂患者。某一天,克拉克分裂成了一个学生,在做题。 突然一道难题难到了克拉克,这道题是这样的: 给你n个数,要求选一些数(可以不选),把它们加起来,使得和恰好是p的倍数(0也是p的倍数),求方案数。 对于n很小的时候,克拉克是能轻易找到的。然而对于n很大的时候,克拉克没有办法了,所以来求助于你。
输入描述
第一行一个整数T(1≤T≤10),表示数据的组数。 每组数据第一行是两个正整数n,p(1≤n,p≤1000)。 接下来的一行有n个整数ai(∣ai∣≤109),表示第i个数。
输出描述
对于每组数据,输出一个整数,表示问题的方案数,由于答案很大,所以求出对109+7的答案即可。
输入样例
1 2 3 1 2
输出样例
2
Hint
有两种方案:什么也不选;全都选。
/****************************************************/
好吧,那只能转换思路了,要使相加之和的结果是p的倍数,就是说%p==0,那么我们不妨一开始就将ai处理成<p的数,以便用数组进行操作
剩下的工作就是用dp解决问题了,转移方程是前i个数,模p为j的方案数dp[i][j]=dp[i-1][j]+dp[i-1][(j-a[i]+p)%p]
即第i个数不取的情况,和取了第i个数的情况。
①若第i个数不取,那么dp[i][j]=dp[i-1][j],不影响方案数;
②若取了第i个数,就相当于前i-1个数相加模p的值为dp[i-1][(j-a[i]+p)%p],即减去第i项值a[i]
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 1001;
const int inf = 1000000000;
const int mod = 1000000007;
__int64 s[N][N];
int a[N];
int main()
{
int t,n,p,i,j;
scanf("%d",&t);
while(t--)
{
memset(s,0,sizeof(s));
scanf("%d%d",&n,&p);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]%=p;
}
for(s[0][0]=1,i=1;i<=n;i++)
for(j=0;j<=p;j++)
s[i][j]=(s[i-1][j]+s[i-1][(j-a[i]+p)%p])%mod;
printf("%I64d\n",s[n][0]);
}
return 0;
}
菜鸟成长记