Divisible Group Sums
Divisible Group Sums
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.
Sample Input
2
10 2
1
2
3
4
5
6
7
8
9
10
5 1
5 2
5 1
2
3
4
5
6
6 2
Sample Output
Case 1:
2
9
Case 2:
1
分析:dp[i][j][k][t]表示前i个选了j个%k=t的方案数,转移一下即可;
本质是一个01背包;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
const int N=2e2+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,a[N],cas;
ll dp[N][][][];
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
int q;
memset(dp,,sizeof(dp));
rep(i,,)dp[][][i][]=;
scanf("%d%d",&n,&q);
rep(i,,n)scanf("%d",&a[i]);
rep(i,,n)rep(j,,min(i,))rep(k,,)rep(m,,k-)
{
dp[i][j][k][m]+=dp[i-][j][k][m];
if(j)dp[i][j][k][m]+=dp[i-][j-][k][((m-a[i])%k+k)%k];
}
printf("Case %d:\n",++cas);
while(q--)
{
int b,c;
scanf("%d%d",&b,&c);
printf("%lld\n",dp[n][c][b][]);
}
}
return ;
}
Divisible Group Sums的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- 一位学长的ACM总结(感触颇深)
发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...
- lightoj刷题日记
提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...
- [Swift]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
随机推荐
- 杂项-Java:JMX
ylbtech-杂项-Java:JMX 1.返回顶部 1. JMX(Java Management Extensions,即Java管理扩展)是一个为应用程序.设备.系统等植入管理功能的框架.JMX可 ...
- 洛谷P1387最大正方形(dp,前缀和)
题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m ...
- Boost(1.69.0) windows入门(译)
目录 Boost windows入门 1. 获得Boost源代码 2. Boost源代码组织 The Boost Distribution 3. 仅用头文件的库 Header-Only Librari ...
- ORA-01075: you are currently logged on
[root@hear01 ~]# su - oracle[oracle@hear01 ~]$ sqlplus "/as sysdba" SQL*Plus: Release 11.2 ...
- 【LeetCode】467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- Java系列学习(十一)-内部类
1.内部类 (1)把类定义在另一个类的内部,该类就称为内部类 (2)内部类的访问规则 A:内部类可以直接访问外部类的成员,包括私有 B:外部类要想访问内部类的成员,必须创建对象 (3)内部类的分类 A ...
- OFDM同步算法之Schmidl算法
Schmidl算法代码 算法原理 训练序列结构 T=[A A],其中A表示复伪随机序列PN,进行N/2点ifft变换得到的符号序列 \[M(d)=\frac{\left | P(d) \right | ...
- [ Nowcoder Contest 165 #D ] 合法括号序列
\(\\\) \(Description\) 键盘上有三个键,敲击效果分别是: 在输出序列尾部添加一个左括号 在输出序列尾部添加一个右括号 删除输出序列尾部的第一个元素,若输出序列为空,则什么都不发生 ...
- 自己整理的css3动画库,附下载链接
动画调用语法 animation: bounceIn 0.3s ease 0.2s 1 both; 按顺序解释参数: 动画名称 如:bounceIn 一周期所用时间 如:0.3s 速度曲线 如:eas ...
- Android 检查手机上是否安装了指定的软件(根据包名检测)
Android检查手机上是否安装了指定的软件(根据包名检测) /** * 检查手机上是否安装了指定的软件 * @param context * @param packageName * @return ...