hdu 5464 Clarke and problem (BestCoder Round #56 (div.2))

Clarke and problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 350    Accepted Submission(s): 151


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

翻译:
问题描述
克拉克是一名人格分裂患者。某一天,克拉克分裂成了一个学生,在做题。 
突然一道难题难到了克拉克,这道题是这样的:  
给你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

解题思路:
官方提示:

Clarke and problem

d(i, j)d(i,j)表示前ii个数,模ppjj的方案数,则容易得到d(0, 0)=1, d(i, j)=d(i-1, j)+\sum_{j=0}^{p-1} d(i-1, (j-a[i]) \ mod \ p)d(0,0)=1,d(i,j)=d(i1,j)+j=0p1d(i1,(ja[i]) mod p),很多人没1a是因为没注意|a_i| \le 10^9ai109

个人认为这其实就是一个dp的题,因为以前做过这样类似的,所以1 A
哈哈,上代码:
/**
2015 - 09 - 20 晚上

Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
const int maxn = 1005;
const int mod = 1e9+7;
const double eps = 1e-7;
int arr[maxn], dp[maxn], fac[maxn];
///arr数组保证每个值都是k的余数( <k 的)
int main()
{
    int T, m, p, k;
    cin>>T;
    while(T--)
    {
        memset(dp, 0, sizeof(dp));
        memset(fac, 0, sizeof(fac));
        dp[0] = 1;
        cin>>m>>p;
        for(int i=0; i<m; i++)
        {
            cin>>k;
            k %= p;
            if(k < 0)
                k += p;
            arr[i] = k;
        }
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<p; j++)
                fac[j] = dp[j];///这句很重要啊
            for(int j=0; j<p; j++)
            {
                k = (j+arr[i])%p;
                fac[k] += dp[j];
            }
            for(int j=0; j<p; j++)
                dp[j] = fac[j]%mod;
        }
        cout<<dp[0]<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值