原题链接: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+7109+710^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很大的时候,克拉克没有办法了,所以来求助于你。

题解

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

接下来,我们用二维数组dp[i][j]dp[i][j]dp[i][j]表示前iii个数可以凑出余数为j" role="presentation" style="position: relative;">jjj的方案数,那么就有状态转移方程:

dp[i][j]=dp[i−1][j]+dp[i−1][(j−x[i]+p) mod p]dp[i][j]=dp[i−1][j]+dp[i−1][(j−x[i]+p)modp]

dp[i][j]=dp[i-1][j]+dp[i-1][(j-x[i]+p)\ mod\ p]

具体来讲,我们将每个数分为两个状态:
1.选了第iii个数,那么方案数就是前i−1" role="presentation" style="position: relative;">i−1i−1i-1个数凑出余数为(j−x[i]+p)(j−x[i]+p)(j-x[i]+p)%p的方案数。
2.不选,那么显然跟dp[i−1][j]dp[i−1][j]dp[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;
}

HDU5464 Clarke and problem相关推荐

  1. bestcoder #56 div 2 B Clarke and problem(dp)

    Clarke and problem  Accepts: 169  Submissions: 372  Time Limit: 2000/1000 MS (Java/Others)  Memory L ...

  2. 动态规划_ 选出一些数相加,问最后是m的倍数的方案有多少种

    题意:给定n个数,让你判断是否能从中选出一些数相加,使得他们的和能够整除m: 分析:这类dp状态转移感觉比较好想,可以从第一项开始每次都把他的结果记录下来,然后轮到后面某一项的时候,直接利用前面的结果 ...

  3. linux下yum错误:[Errno 14] problem making ssl connection Trying other mirror.

    所有的base 都要取消注释 mirrorlist 加上注释 另外所有的enable都要设为零 目录 今天是要yum命令安装EPEL仓库后 yum install epel-release 突然发现y ...

  4. A + B Problem

    1001: A + B Problem Description 计算 A + B. Input 多组测试数据,每组测试数据占一行,包括2个整数. Output 在一行中输出结果. Sample Inp ...

  5. Error:(49, 1) A problem occurred evaluating project ':guideview'. Could not read script 'https://r

    出现问题如下: Error:(49, 1) A problem occurred evaluating project ':guideview'. > Could not read script ...

  6. #418 Div2 Problem B An express train to reveries (构造 || 全排列序列特性)

    题目链接:http://codeforces.com/contest/814/problem/B 题意 : 有一个给出两个含有 n 个数的序列 a 和 b, 这两个序列和(1~n)的其中一个全排列序列 ...

  7. ADPRL - 近似动态规划和强化学习 - Note 3 - Stochastic Infinite Horizon Problem

    Stochastic Infinite Horizon Problem 3.Stochastic Infinite Horizon Problem 定义3.1 无限范围的马尔可夫决策过程 (Marko ...

  8. ADPRL - 近似动态规划和强化学习 - Note 2 - Stochastic Finite Horizon Problem

    2. Stochastic Finite Horizon Problem 在这一节中主要介绍了随机DP算法来解决不确定性下的有限地范围问题,如Denition 1.4所述,它被表述为一个组合优化问题. ...

  9. There was a problem confirming the ssl certificate ……

    在安装一个Python库onetimepass时发生下面的问题: pip install onetimepass Could not fetch URL https://pypi.python.org ...

  10. HDU 1757 A Simple Math Problem

    Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x & ...

最新文章

  1. 将现有Git存储库推送到SVN
  2. APP漏洞自动化扫描专业评测报告
  3. 【2018北京集训(六)】Lcm
  4. datefromstring 转换不准确_免费的在线OCR工具,将图片内容转换为文本内容
  5. PHP memcached
  6. 数学建模7 多元线性回归分析
  7. [Android5 系列—] 2. 开始另外一个活动
  8. docker build命令详解_Docker镜像与容器常用命令图文详解
  9. 国内银行CNAPS CODE 查询
  10. java车辆使用管理系统_JAVA车辆管理系统,基于SSM框架的系统
  11. scara机器人动荷载_揭密SCARA机器人
  12. Js坐标转换器-百度地图坐标转腾讯地图坐标
  13. 【转】告诉你外语学习的真实方法及误区分析(精编版)-part 3
  14. FinTech4Good创始人张晓晨:全球区块链的产业应用趋势
  15. 大学计算机考试word实操夏威夷,计算机一级等级考试操作题WORD(4页)-原创力文档...
  16. 软件工程如何选择方向
  17. 《正则表达式深入浅出》开放下载,快快收藏!
  18. Google 开源的项目集合
  19. c++ set使用(增删查遍历)
  20. 【Python自动化办公】批量将Excel表格数据导出为PDF文件

热门文章

  1. 210.课程表II(力扣leetcode) 博主可答疑该问题
  2. java 接口 同名方法_java 实现多个接口 方法重名的解决办法——内部类
  3. 云服务器操作系统新手,云服务器操作系统新手
  4. 更改input标签的placeholder的样式
  5. JVM内存模型及垃圾回收机制
  6. MongoDB自定义函数部分 定义及引用
  7. Unity3d之HashSlash学习笔记(一)--角色属性类的构架
  8. C# 14位日期型字符串yyyyMMddHHmmss转变为日期格式
  9. 习题9.38 查找字符串中的数字、字母
  10. elasticsearch+kibana 日志系统配置java日志解析和过滤无用字段