[UVA][dp] 12001 - UVa Panel Discussion@Morris' Blog|PChome Online 人新台
2013-10-09 07:51:36| 人1,588| 回0 | 上一篇 | 下一篇

[UVA][dp] 12001 - UVa Panel Discussion

0 收藏 0 0 站台


  UVa Panel Discussion 

The UVa online judge team is arranging a panel discussion for the next ACM-ICPC World Finals event in Orlando, Florida. They want that three or four of the contestants take part in the panel and as they have about 300 persons for selecting such a little group, they have decided to put some restrictions in order to reduce the number of possibilities.

After thinking about several options, they finally propose that in case the number of contestants to choice be 3, all of them must be of the same country or from three different countries; and in case the number be 4, at least three of them will be of the same country or must be from at least three different countries.

Could you help them to calculate the number of different selections they can make following the restrictions above.

Input 

The input file contains several test cases; each of them consists of two lines.

The first contains two integers N and M separated by one space. N ( 3$ le$N$ le$300) is the number of contestants and M ( 1$ le$M$ le$50) the total number of different countries. The second line consists of N integers between 1 and M, separated by a space, representing the country each contestant is from (It is not necessary that contestants will be from M countries).

Last line of the input will contain two zeroes and it won't be processed.

Output 

For each input case write, in a line by itself, two integers separated by a space.

The first integer being be the number of ways to select a group of three people, and the second the number of ways to do it of four people.

Sample Input 

3 5 5 4 2 5 3 3 1 3 2 2 10 10 1 8 9 1 6 7 3 4 10 4 0 0 

Sample Output 

1 0 4 4 104 209 




目解法:


dp[i][j][k] 表示放入 i 人,分中有 j 家,k 人。

特考 ooxx 的移。

#include <stdio.h>
#include <string.h>
int main() {
    int n, m x;
    int i, j, k;
    int C[305][5] = {};
    C[0][0] = 1;
    for(i = 1; i <= 300; i++) {
        C[i][0] = 1;
        for(j = 1; j <= i && j < 5; j++) {
            C[i][j] = C[i-1][j]+C[i-1][j-1];
        }
    }
    while(scanf("%d %d", &n, &m) == 2 && n+m) {
        int country[55] = {};
        for(i = 0; i < n; i++) {
            scanf("%d", &x);
            country[x]++;
        }
        int dp[55][5][5] = {};
        dp[0][0][0] = 1;
        for(i = 1; i <= m; i++) {
            dp[i][0][0] = 1;
            for(j = 1; j <= 4; j++) {//different country.
                for(k = 1; k <= 4; k++) {//people
                    dp[i][j][k] = dp[i-1][j][k];
                    if(k-1 >= 0)
                        dp[i][j][k] += dp[i-1][j-1][k-1]*C[country[i]][1];
                    if(k-2 >= 0 && !(j-1 == 1 && k-2 == 2))
                        dp[i][j][k] += dp[i-1][j-1][k-2]*C[country[i]][2];
                    if(k-3 >= 0)
                        dp[i][j][k] += dp[i-1][j-1][k-3]*C[country[i]][3];
                    if(k-4 >= 0)
                        dp[i][j][k] += dp[i-1][j-1][k-4]*C[country[i]][4];
                }
            }
        }
        int ret3 = dp[m][1][3]+dp[m][3][3];
        int ret4 = dp[m][1][4]+dp[m][2][4]+dp[m][3][4]+dp[m][4][4];
        printf("%d %d\n", ret3, ret4);
    }
    return 0;
}
/*
10 10
1 1 3 4 4 6 7 8 9 10
*/

台: Morris
人(1,588) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 教育(修、留、研究、教育概) | 人分: UVA |
此分下一篇:[UVA][二分搜] 10372 - Leaps Tall Buildings
此分上一篇:[UVA][] 12202 - Haunted Graveyard

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86