HDU5464 Clarke and problem

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=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 10 9 + 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).

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.

题目大意

克拉克是一名人格分裂患者。某一天,克拉克分裂成了一个学生,在做题。
突然一道难题难到了克拉克,这道题是这样的:

给你n个数,要求选一些数(可以不选),把它们加起来,使得和恰好是p的倍数(0也是p的倍数),求方案数。

对于n很小的时候,克拉克是能轻易找到的。然而对于n很大的时候,克拉克没有办法了,所以来求助于你。

题解

我们注意到 p1000 p ⩽ 1000 ,但是数据范围是 109 10 9 ,背包显然不可做。但是,考虑到我们只需要凑出p的倍数,那么dp就只跟模p的余数有关系,那么我们读入时就可以将每个数模p缩小数据范围。

接下来,我们用二维数组 dp[i][j] d p [ i ] [ j ] 表示前 i i 个数可以凑出余数为j的方案数,那么就有状态转移方程:

dp[i][j]=dp[i1][j]+dp[i1][(jx[i]+p) mod p] d p [ i ] [ j ] = d p [ i − 1 ] [ j ] + d p [ i − 1 ] [ ( j − x [ i ] + p )   m o d   p ]
   

具体来讲,我们将每个数分为两个状态:
1.选了第 i i 个数,那么方案数就是前i1个数凑出余数为 (jx[i]+p) ( j − x [ i ] + p ) 的方案数。
2.不选,那么显然跟 dp[i1][j] d p [ i − 1 ] [ j ] 一样。

代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int M=1005,mod=1e9+7;
int x[M],n,p;
ll dp[M][M];
void in()
{
    memset(dp,0,sizeof(dp));
    scanf("%d%d",&n,&p);
    for(int i=1;i<=n;++i)
    scanf("%d",&x[i]),x[i]%=p;
}
void ac()
{
    dp[0][0]=1;
    for(int i=1;i<=n;++i)
    for(int j=0;j<=p;++j)
    dp[i][j]=(dp[i-1][j]+dp[i-1][(j-x[i]+p)%p])%mod;
    printf("%lld\n",dp[n][0]);
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int i=1;i<=T;++i)
    in(),ac();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值