HDU 5464 Clarke and problem(dp 动态规划)——BestCoder Round #56(div.1 div.2)

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
 

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.
 

Source
 

/************************************************************************/

附上该题对应的中文题

Clarke and problem

 
 
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
克拉克是一名人格分裂患者。某一天,克拉克分裂成了一个学生,在做题。 
突然一道难题难到了克拉克,这道题是这样的:  
给你nn个数,要求选一些数(可以不选),把它们加起来,使得和恰好是pp的倍数(00也是pp的倍数),求方案数。  
对于nn很小的时候,克拉克是能轻易找到的。然而对于nn很大的时候,克拉克没有办法了,所以来求助于你。  
输入描述
第一行一个整数T(1 \le T \le 10)T(1T10),表示数据的组数。  
每组数据第一行是两个正整数n, p(1 \le n, p \le 1000)n,p(1n,p1000)。  
接下来的一行有nn个整数a_i(|a_i| \le 10^9)ai(ai109),表示第ii个数。
输出描述
对于每组数据,输出一个整数,表示问题的方案数,由于答案很大,所以求出对10^9+7109+7的答案即可。  
输入样例
1
2 3
1 2
输出样例
2
Hint
有两种方案:什么也不选;全都选。
/****************************************************/
解题思路:一开始的时候理解得有点问题,导致还想把n个数所能得到的所有相加之和都给算出来,然而仔细一想,毫无可行性,毕竟随着n的增大,相加之和的可能性是指数爆炸的。

好吧,那只能转换思路了,要使相加之和的结果是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;
}
菜鸟成长记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值