24h購物| | PChome| 登入
2012-02-27 13:50:46| 人氣1,066| 回應0 | 上一篇 | 下一篇

[UVA] 10616 - Divisible Group Sums

推薦 0 收藏 0 轉貼0 訂閱站台

Problem H

Divisible Group Sums

Input: Standard Input

Output: Standard Output

Time Limit: 1 Second

 

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

The input file contains maximum ten sets of inputs. The description of each set is given below.

 

The first line of each set 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 no of query. Each of the next N lines contains one 32 bit signed integer. Our queries will have to be answered based on these N numbers. Next Q lines contain Q queries. Each query contains two integers D (0<D<=20) and M (0<M<=10) whose meanings are explained in the first paragraph.

 

Input is terminated by a case whose N=0 and Q=0. This case should not be processed.

 

Output

For each set of input, print the set number. Then for each query in the set print the query number followed by the number of desired groups. See sample output to know the exact output format.

 

Sample Input                             Output for Sample Input

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

SET 1:

QUERY 1: 2

QUERY 2: 9

SET 2:

QUERY 1:






做法 by http://www.cnblogs.com/staginner/archive/2011/12/17/2291386.html

#include <stdio.h>
#include <string.h>

long long A[201], DP[201][16][21];
void solve(int N, int M, int D) {
    memset(DP, 0, sizeof(DP));
    int i, j, k;
    long long tmp;
    DP[0][0][0] = 1;
    for(i = 1; i <= N; i++) {
        for(j = 0; j <= M; j++) {
            for(k = 0; k < D; k++) {
                tmp = (k+A[i])%D;
                if(tmp < 0) tmp += D;
                DP[i][j][k] += DP[i-1][j][k];
                if(j) {
                    DP[i][j][k] += DP[i-1][j-1][tmp];
                }
            }
        }
    }
    printf("%lld\n", DP[N][M][0]);
}
int main() {
    int N, Q, D, M, i;
    int Case = 0;
    while(scanf("%d %d", &N, &Q) == 2) {
        if(N == 0 && Q == 0)    break;
        for(i = 1; i <= N; i++)
            scanf("%lld", &A[i]);
        printf("SET %d:\n", ++Case);
        for(i = 1; i <= Q; i++) {
            scanf("%d %d", &D, &M);
            printf("QUERY %d: ", i);
            solve(N, M, D);
        }
    }
    return 0;
}

台長: Morris
人氣(1,066) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11407 - Squares
此分類上一篇:[UVA] 10664 - Luggage

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文