lightoj 1125 - Divisible Group Sums (dp)
Given a list of N numbers you will be allowed to choose any M of them. So you can choose in NCM ways. You will have to determine how many of these chosen groups have a sum, which is divisible by D.
Input
Input starts with an integer T (≤ 20), denoting the number of test cases.
The first line of each case contains two integers N (0 < N ≤ 200) and Q (0 < Q ≤ 10). Here N indicates how many numbers are there and Q is the total number of queries. Each of the next N lines contains one 32 bit signed integer. The queries will have to be answered based on these N numbers. Each of the next Q lines contains two integers D (0 < D ≤ 20) and M (0 < M ≤ 10).
Output
For each case, print the case number in a line. Then for each query, print the number of desired groups in a single line.
题意:给你n个数,提出q的问题,问你从n个数取出m个求和能被d整除的有几个。
我们设一下dp[i][j][mod]表示取到第i个数,取了j位数余数为mod的方案数。显然我们可以得到递推公式,
dp[i][j][mod]+=dp[i-1][j][mod](这一位数不取)or dp[i][j][((mod+a[i])%d+d)%d]+=dp[i-1][j-1][mod]。
((mod+a[i])%d+d)%d这样取余是为了处理负数的出现。
#include <iostream>
#include <cstring>
using namespace std;
int a[220];
long long dp[220][30][30];
int main()
{
int t;
cin >> t;
int ans = 0;
while(t--) {
ans++;
int n , q;
cin >> n >> q;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
}
cout << "Case " << ans << ":" << endl;
for(int i = 0 ; i < q ; i++) {
int d , m;
cin >> d >> m;
memset(dp , 0 , sizeof(dp));
for(int i = 0 ; i <= n ; i++) {
dp[i][0][0] = 1;
}
for(int i = 1 ; i <= n ; i++) {
for(int j = 1 ; j <= m ; j++) {
for(int l = 0 ; l < d ; l++) {
dp[i][j][l] += dp[i - 1][j][l];
dp[i][j][((l + a[i]) % d + d) % d] += dp[i - 1][j- 1][l];
}
}
}
cout << dp[n][m][0] << endl;
}
}
return 0;
}
lightoj 1125 - Divisible Group Sums (dp)的更多相关文章
- Light oj 1125 - Divisible Group Sums (dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1125 题意: 给你n个数,q次询问,每次询问问你取其中m个数是d的整数倍的方案 ...
- LightOJ1125 Divisible Group Sums
Divisible Group Sums Given a list of N numbers you will be allowed to choose any M of them. So you c ...
- Divisible Group Sums
Divisible Group Sums Given a list of N numbers you will be allowed to choose any M of them. So you c ...
- LightOJ1125 Divisible Group Sums(DP)
题目问从N个数中取出M个数,有多少种取法使它们的和能被D整除. dp[i][j][k]表示,前i个数取出j个数模D的余数为k的方案数 我用“我为人人”的方式来转移,就从i到i+1转移,对于第i+1个数 ...
- UVa 10616 - Divisible Group Sums
称号:给你n数字.免去m一个,这使得他们可分割d.问:有多少种借贷. 分析:dp,D01背包. 背包整数分区. 首先.整点d.则全部数字均在整数区间[0,d)上: 然后,确定背包容量,最大为20*10 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- [Codeforces626F] Group Projects (DP)
Group Projects Description There are n students in a class working on group projects. The students w ...
- LightOJ 1422 Halloween Costumes 区间dp
题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多少条衣服才能参加所有宴会 思路:dp[i][j]代表i-j天最少要带的衣服 从后向前dp 区间从大到小 更新d ...
随机推荐
- HTML 第5章CSS3美化网页元素
<span>标签: <span>标签是用来组合HTML文档中的行内元素,它没有固定的格式表示. 字体样式: 属性名 ...
- Pandas随机采样
实现对DataFrame对象随机采样 pandas是基于numpy建立起来的,所以numpy大部分函数可作用于DataFrame和Series数据结构. numpy.random.permutatio ...
- Cobbler 自动安装CentOS7
1. Cobbler介绍 Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装.重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等.Cobbler可以使用命 ...
- Power Designer导出模型的sql加注释-Oracle语句
第一步:Database-->Edit Current DBMS 第二步: 然后分别将 Script-->Objects-->Table-->TableComment Scri ...
- Java基础的一些知识点(一):接口interface
1.接口的含义 接口可以理解成统一的协议, 而接口中的属性也属于协议中的内容.但是接口的属性都是公共的,静态的,最终的. 接口的成员特点: 1.成员变量只能是常量,默认修饰符 public stati ...
- Linux curl 表单登录或提交与cookie使用
本文主要讲解通过curl 实现表单提交登录.单独的表单提交与表单登录都差不多,因此就不单独说了. 说明:针对curl表单提交实现登录,不是所有网站都适用,原因是有些网站后台做了限制或有其他校验.我们不 ...
- SprintBoot
简述 推出时间:从Maven仓库的时间看是2016.7.28 目的:摆脱大量的XML配置文件以及复杂的Bean依赖关系,快速.敏捷地开发新一代基于Spring框架的应用程序 思想:约定优于配置(con ...
- Mysql 分页order by一个相同字段,发现顺序错乱
两次分页查询,其中跳过了2个id select * from jdp_tb_trade where jdp_modified>='2017-04-24 20:22:01' and jdp_ ...
- Maven多模块项目打包前的一些注意事项(打包失败)
一. 最近在打包Maven项目时遇到了点问题,这个项目是Maven多模块项目,结构如下: projectParent├── xxxx-basic├── xxxx-web1├── xxxx-collec ...
- [转载]MongoDB管理基础
1. 启动和停止MongoDB: 执行mongod命令启动MongoDB服务器.mongod有很多可配置的选项,我们通过mongod --help可以查看所有选项,这里仅介绍一些主要选项: - ...