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)的更多相关文章

  1. Light oj 1125 - Divisible Group Sums (dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1125 题意: 给你n个数,q次询问,每次询问问你取其中m个数是d的整数倍的方案 ...

  2. 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 ...

  3. 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 ...

  4. LightOJ1125 Divisible Group Sums(DP)

    题目问从N个数中取出M个数,有多少种取法使它们的和能被D整除. dp[i][j][k]表示,前i个数取出j个数模D的余数为k的方案数 我用“我为人人”的方式来转移,就从i到i+1转移,对于第i+1个数 ...

  5. UVa 10616 - Divisible Group Sums

    称号:给你n数字.免去m一个,这使得他们可分割d.问:有多少种借贷. 分析:dp,D01背包. 背包整数分区. 首先.整点d.则全部数字均在整数区间[0,d)上: 然后,确定背包容量,最大为20*10 ...

  6. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  7. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  8. [Codeforces626F] Group Projects (DP)

    Group Projects Description There are n students in a class working on group projects. The students w ...

  9. LightOJ 1422 Halloween Costumes 区间dp

    题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多少条衣服才能参加所有宴会 思路:dp[i][j]代表i-j天最少要带的衣服 从后向前dp 区间从大到小 更新d ...

随机推荐

  1. hdoj 4762 Cut the Cake

    题意很简单就不说了. 解题的关键就是这个公式 answer=n/(m^(n-1)); 要用到大数的乘法.然后java水过. import java.util.*; import java.math.* ...

  2. win10去除快捷方式小箭头

    切忌删除注册表项: HKEY_CLASSES_ROOT -> lnkfile -> IsShortcut 这个方法以前是可以的,但是在2018年之后更新的系统就会出现任务栏图标打不开的情况 ...

  3. Sring 的 @AliasFor 使用规则

    一.该标签存在的意义 顾名思义 @AliasFor 表示别名,它可以注解到自定义注解的两个属性上,表示这两个互为别名,也就是说这两个属性其实同一个含义.该标签存在的含义,从网上查发现有个点, 若  自 ...

  4. Jenkins 配置 SpringBoot 自动构建部署

    服务器版本 Linux version 3.10.0-957.12.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8 ...

  5. java常见面试题目(二)

    部分没有答案可以自行百度. 1.myeclipse与eclipse的区别. 2.说说对maven或者SVN的理解. 3.类的加载过程 (创建对象的过程)  1)子父类里静态属性 赋上默认初始值 如果有 ...

  6. java并发编程(十六)----(线程池)java线程池的使用

    上节我们简单介绍了线程池,这次我们就来使用一下.Executors提供四种线程池,分别是:newCachedThreadPool,newFixedThreadPool ,newScheduledThr ...

  7. Java.基础 -------- 一个Java源文件中可以有很多类,但只能有一个类是public的

    一个Java源文件中可以有很多类,但只能有一个类是public的        Java程序是从一个public类main函数开始执行的,只能有一个public是为了给类装载器提供方便,一个publi ...

  8. IntelliJ IDEA 激活(最新)

    注:此文以 Mac 为例,Windows 的激活方法也大同小异.如果不差钱的话,建议购买正版! 1.下载安装 直接通过下面的链接到官网下载最新的 Ultimate 版本即可: https://www. ...

  9. java并发系列 - 第28天:实战篇,微服务日志的伤痛,一并帮你解决掉

    这是java高并发系列第28篇文章. 环境:jdk1.8. 本文内容 日志有什么用? 日志存在的痛点? 构建日志系统 日志有什么用? 系统出现故障的时候,可以通过日志信息快速定位问题,修复bug,恢复 ...

  10. nginx之gzip压缩提升网站速度

    目录: 为啥使用gzip压缩 nginx使用gzip gzip的常用配置参数 nginx配置gzip 注意 为啥使用gzip压缩 开启nginx的gzip压缩,网页中的js,css等静态资源的大小会大 ...