lightoj 1125 - Divisible Group Sums

来源:互联网 发布:数据泄露防护系统 编辑:程序博客网 时间:2024/05/22 03:39

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

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

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

Case 1:

2

9

Case 2:

1



输入N和q,代表有N个数,q次询问,每次询问输入两个数,D和M,代表从N个数中选M个数的和可以被D整除,问有多少种方案。

dp[i][j][k]代表前i个数中选j个,它们的和模D为k。


#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define inf 1e9#define endl '\n'#define LL long longusing namespace std;LL dp[210][15][25];int main(void){    int T,n,m,q,d,i,j,k;    int a[205];    scanf("%d",&T);    int cas = 1;    while(T--)    {        scanf("%d%d",&n,&q);        for(i=1;i<=n;i++)            scanf("%d",&a[i]);        printf("Case %d:\n",cas++);        while(q--)        {            scanf("%d%d",&d,&m);            memset(dp,0,sizeof(dp));            dp[0][0][0] = 1;            for(i=1;i<=n;i++)            {                dp[i][0][0] = 1;                for(j=1;j<=m;j++)                {                    for(k=0;k<d;k++)                    {                        dp[i][j][k] += dp[i-1][j][k];                        dp[i][j][k] += dp[i-1][j-1][(k - a[i]%d + d)%d];                    }                }            }            printf("%lld\n",dp[n][m][0]);        }    }}


0 0
原创粉丝点击