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

从n个数字里面取m个有C(n,m)种取法,问有多少种取法使得总和是D的倍数

因为询问好多组(m,d),所以每次都要对不同的d先取模,然后显然就是个背包啦,数字不超过d,只取m个,最大容量只有200

然后特么给的ai还有负数,,,我真是,,,

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<deque>
 9 #include<set>
10 #include<map>
11 #include<ctime>
12 #define LL long long
13 #define inf 0x7ffffff
14 #define pa pair<LL,int>
15 #define mkp(a,b) make_pair(a,b)
16 using namespace std;
17 inline LL read()
18 {
19     LL x=0,f=1;char ch=getchar();
20     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
21     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
22     return x*f;
23 }
24 int n,q;
25 int a[210];
26 int b[210];
27 LL f[210][210];
28 inline void work(int cur)
29 {
30     printf("Case %d:\n",cur);
31     n=read();q=read();
32     for (int i=1;i<=n;i++)a[i]=read();
33     for (int i=1;i<=q;i++)
34     {
35         int mod=read(),m=read();
36         memset(f,0,sizeof(f));f[0][0]=1;
37         for (int j=1;j<=n;j++)b[j]=(a[j]%mod+mod)%mod;
38         for (int j=1;j<=n;j++)
39         {
40             for (int k=m;k>=1;k--)
41                 for (int l=m*mod;l>=b[j];l--)
42                 f[k][l]+=f[k-1][l-b[j]];
43         }
44         LL sum=0;
45         for (int j=0;j<=m*mod;j+=mod)sum+=f[m][j];
46         printf("%lld\n",sum);
47     }
48 }
49 int main()
50 {
51     int T=read(),tt=0;
52     while (T--)work(++tt);
53 }

LightOJ 1125

时间: 2024-10-05 18:13:49

LightOJ1125 Divisible Group Sums的相关文章

LightOJ1125 Divisible Group Sums(DP)

题目问从N个数中取出M个数,有多少种取法使它们的和能被D整除. dp[i][j][k]表示,前i个数取出j个数模D的余数为k的方案数 我用“我为人人”的方式来转移,就从i到i+1转移,对于第i+1个数有取和不取两种选择,然后确定j和k这两个维度的情况. 另外题目说数字是32位有符号整数,所以是会出现负数的...模D之后加D再模D就行了. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 long long

Light oj 1125 - Divisible Group Sums (dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1125 题意: 给你n个数,q次询问,每次询问问你取其中m个数是d的整数倍的方案数. 题意: dp[i][j][k] 表示前i个数, %d=j, 取了k个的方案数. ID SUBMISSION TIME PROBLEM SOURCE CPU MEMORY VERDICT 839200 2016-10-15 14:59:00 1125 - Divisible Group Sums

UVA10616 - Divisible Group Sums(dp)

题目链接 题目大意:N个数,选择m个数出来,问相加的和能够整除MOD有多少种选择方式. 解题思路:从1到N数选择过去,每个数有选和不选两种可能,并且(num + d) % MOD = num % MOD + d % MOD, 所以可以这么做,最后判断一下余数等于0么.坑点是这题N个数会有负数,负数的取模 (num % MOD + MOD) % MOD.这一题因为没有控制选择的数目m的增加次数,导致re了好久,简直无语死了... 代码: #include <cstdio> #include &l

UVa 10616 - Divisible Group Sums

题目:给你n个数字,从中取出m个,使得他们的和能整除d,问有多少种取法. 分析:dp,二维01背包.整数拆分用背包. 首先,将所有的点对d取余数,则所有数字均在整数区间[0,d)上: 然后,确定背包容量,最大为20*10 = 200,计算二维01背包: 最后,求出所有能整除d的整数取法的和即可. 说明:注意使用long long防止溢出:注意数据中的负数. #include <algorithm> #include <iostream> #include <cstdlib&g

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

djfhkjdahsg 将会对会计师公会斯蒂芬

http://f.dangdang.com/group/24690/7818358/ http://f.dangdang.com/group/24690/7818366/ http://f.dangdang.com/group/24690/7818410/ http://f.dangdang.com/group/24690/7818420/ http://f.dangdang.com/group/24690/7818408/ http://f.dangdang.com/group/24690/7

供应科顾客顾客顾客

http://f.dangdang.com/group/24554/3373214/http://f.dangdang.com/group/24554/3373218/http://f.dangdang.com/group/24554/3373222/http://f.dangdang.com/group/24554/3373227/http://f.dangdang.com/group/24554/3373230/http://f.dangdang.com/group/24554/337323

放假放假放假凤凰男

http://f.dangdang.com/group/24554/3373214/http://f.dangdang.com/group/24554/3373218/http://f.dangdang.com/group/24554/3373222/http://f.dangdang.com/group/24554/3373227/http://f.dangdang.com/group/24554/3373230/http://f.dangdang.com/group/24554/337323

[Swift Weekly Contest 118]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 divisible by K. Example 1: Input: A = [4,5,0,-2,-3,1], K = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by K = 5: [4, 5, 0, -2