HDU 5654 Clarke and problem(动态规划)
2015-09-24 20:50
239 查看
题目链接:
戳我题目大意:
http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=631&pid=1002问题描述
克拉克是一名人格分裂患者。某一天,克拉克分裂成了一个学生,在做题。 突然一道难题难到了克拉克,这道题是这样的: 给你nn个数,要求选一些数(可以不选),把它们加起来,使得和恰好是pp的倍数(00也是pp的倍数),求方案数。 对于nn很小的时候,克拉克是能轻易找到的。然而对于nn很大的时候,克拉克没有办法了,所以来求助于你。
输入描述
第一行一个整数T(1 \le T \le 10)T(1≤T≤10),表示数据的组数。 每组数据第一行是两个正整数n, p(1 \le n, p \le 1000)n,p(1≤n,p≤1000)。 接下来的一行有nn个整数a_i(|a_i| \le 10^9)ai(∣ai∣≤109),表示第ii个数。
输出描述
对于每组数据,输出一个整数,表示问题的方案数,由于答案很大,所以求出对10^9+7109+7的答案即可。
样例解释:
略解题思路:
设 dp[i][j] 代表 前 i 个数 除模余 j 的 个数, 则 dp[0][0] = 1。dp[i][j] = dp[i-1][j] + dp[i-1][t], t必须满足(t + a[i]) % p == j
代码:
//Author LJH //www.cnblogs.com/tenlee #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <map> #define clc(a, b) memset(a, b, sizeof(a)) #define LL long long using namespace std; const int inf = 0x3f; const int INF = 0x3f3f3f3f; const int maxn = 1e3+5; const int M = 1e9+7; int dp[maxn][maxn], a[maxn], n, p; int main() { int T; scanf("%d", &T); while(T--) { scanf("%d %d", &n, &p); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); } clc(dp, 0); dp[0][0] = 1; for(int i = 1; i <= n; i++) { int t = ((p - a[i]) % p + p) % p; for(int j = 0; j < p; j++) { dp[i][j] = (dp[i-1][j] + dp[i-1][t]) % M; t = (t + 1) % p; } } printf("%d\n", dp [0]); } return 0; }
相关文章推荐
- 实战是最好的学习方式
- Java基础知识强化69:基本类型包装类之Character案例(统计字符串中大写小写以及数字的次数)
- 关于iOS国际化(根据手机系统来切换语言) App支持多语言切换
- Android 4.4 中 WebView 使用注意事项
- 设置NavigationBar标题的颜色
- NOIP 2003
- 两种线程创建的比较
- 时差对照表
- 0922第三方应用百度+友盟
- 想写个软件,下面写一下自己的想法。
- 基于Kaggle数据的词袋模型文本分类教程
- ZOJ 3822 Domination 期望dp
- 5W1H 和 鱼骨分析法
- 第13章 线程安全与锁优化
- CNN卷积神经网络总结
- 多叉树的查找(广度优先遍历)
- javascript验证url格式是否合法
- 一些问题
- 印象笔记衰落带来哪些警示
- 《Java编程思想》读书笔记3.操作符