首页 > 代码库 > LightOJ1125 Divisible Group Sums

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

 

LightOJ1125 Divisible Group Sums