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. Thrift框架快速入门

    Thrift介绍1.什么是thrift?thrift早期由facebook内部团队开发,主要用于实现跨语言间的方法调用,属于远程方法调用的一种,后开源纳入apache中,成为了apache thrif ...

  2. 按需制作最小的本地yum源

    [需求背景] 有时候客户的环境里面只能离线安装文件,此时可以使用CentOS的ISO光盘作为本地源进行安装,或者是制作一个包含了YUM源服务的虚拟机. 无论上面的哪一种方式都不够轻量,我们自己的组件可 ...

  3. 01-Spring Security框架学习

    目录 01-Spring Security框架学习 简介 Spring Security 是什么 Spring Security 解决那些问题 Spring Security 的优点 历史背景 Spr ...

  4. oracle RAC LOG_ARCHIVE_DEST_1 与 LOG_ARCHIVE_DEST 冲突解决

    在做 oracle RAC 归档日志配置时,出现了一个错误,开始看资料的时候, 注意到了 LOG_ARCHIVE_DEST_n 与 LOG_ARCHIVE_DEST 不能同时使用, 但在配置的时候并没 ...

  5. hadoop学习(一)----概念和整体架构

    程序员就得不停地学习啊,故步自封不能满足公司的业务发展啊!所以我们要有搞事情的精神.都说现在是大数据的时代,可以我们这些码农还在java的业务世界里面转悠呢.好不容易碰到一个可能会用到大数据技术的场景 ...

  6. LeetCode——372. Super Pow

    题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...

  7. Spring基础笔记

    Spring带给了我们什么便利? 注解版本的IOC如何玩? 组件注册 组件注册的过程中有哪些过滤规则? 如何控制组件的作用域(单例多例)? 六种注册组件的方式? 生命周期 什么是bean的生命周期 在 ...

  8. Docker进阶-容器监控cAdvisor+InfluxDB+Granfana

    概述 前面文章介绍使用docker compose组合应用并利用scale快速对容器进行扩容. 由于docker compose启动的服务都在同一台宿主机上,对于一个宿主机上运行多个容器应用时,容器的 ...

  9. 开源音乐下载神器XMusicDownloader更新,支持歌单一键下载,支持无损音乐

    开源音乐下载神器XMusicDownloader更新啦,新增网易.腾讯音乐歌单歌曲.歌手歌曲.专辑歌曲一键下载,同时支持下载flac无损音乐. 功能 V1.0 功能开源工具软件XMusicDownlo ...

  10. JVM(十一):内存分配

    JVM(十一):内存分配 在前面的章节中,我们花了大量的篇幅去介绍 JVM 内的内存布局.对象在内存中的状态.垃圾回收的算法和具体实现等.今天让我们探讨一下对象是如何分配内存的. 堆内存划分 前面说过 ...