BestCoder Round #56 1002 hdu 5464 (背包问题)
来源:互联网 发布:mysql indexof 编辑:程序博客网 时间:2024/06/06 13:22
Clarke and problem
Accepts: 169
Submissions: 372
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
题目:给你一组数,从中挑一些数,它们的和是p的倍数,求方案数
分析:因为a[i]可以是负数,所以输入时要处理一下。因为是从数组中选择一些数,所以对于每个数可选可不选,这就成了01背包问题。
d[i][j]表示前i个数,构成余数是j的方法总数。
转态转移方程
d[i][j]=(d[i][j]+d[i-1][j])%MOD
d[i][(j+a[i])%p]=(d[i][(j+a[i])%p]+d[i-1][j])%MOD
#include<iostream>#include<string>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;#define LL long longconst int maxn=1005;const int MOD=1e9+7;int a[maxn],d[maxn][maxn];int main(){ int T,n,p,t; cin>>T; while(T--){ scanf("%d%d",&n,&p); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); a[i]%=p; a[i]=(a[i]+p)%p; } memset(d,0,sizeof(d)); d[0][0]=1; for(int i=1;i<=n;i++){ for(int j=0;j<p;j++){ d[i][j]=(d[i][j]+d[i-1][j])%MOD; d[i][(j+a[i])%p]=(d[i][(j+a[i])%p]+d[i-1][j])%MOD; } } printf("%d\n",d[n][0]); }}
0 0
- BestCoder Round #56 1002 hdu 5464 (背包问题)
- BestCoder Round #16 1002(hdu 5087)
- BestCoder Round #82 (div.1) 1002 HDU 5677 dp-类似多重背包的思想
- hdu 5464 Clarke and problem (BestCoder Round #56 (div.2))
- hdu 4990 Reading comprehension(BestCoder Round #8 1002)
- HDU 5666 Segment(BestCoder Round #80 1002)
- HDU 5671 Matrix (BestCoder Round #81 (div.2) 1002)
- hdu 4908 BestCoder Sequence && BestCoder Round #3 1002
- [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
- HDU BestCoder Round #1 1002 项目管理
- HDU 5083 Instruction &&Bestcoder round#15 1002
- [BestCoder] Round #21 1002 - Formula | HDU 5139
- HDU 5281 BestCoder Round #47 1002
- hdu 5591 BestCoder Round #65(博弈)
- HDU 5104 Primes Problem(拆分素数和问题)【BestCoder Round #18 1001】
- HDU 5643 bestcoder Round #75 king's game [威瑟夫问题]
- BestCoder Round #52 (div.2)(hdu 5417,hdu 5418)
- HDU BestCoder Round #48 1001
- Android贴吧系统学习-----悬浮布局的做法
- Ubuntu 输入密码之后闪现一下又回答输入密码界面
- 解决树莓派2 HDMI输出的闪屏问题
- ajax+springmvc不执行success方法
- 闪讯利用openwrt路由器拨号教程(一)
- BestCoder Round #56 1002 hdu 5464 (背包问题)
- 位运算符的注意事项
- SQL扩展之T-SQL中的数据定义语言
- ahu - 572
- MySQL备份与恢复
- 白盒测试
- 【MySQL】查看MySQL配置文件路径及相关配置
- Dozer(JavaBean的映射工具)开发手册
- USACO 1.4 The Clocks (位操作+枚举+BFS)