[UVA][math] 911 - Multinomial Coefficients@Morris' Blog|PChome Online 人新台
2013-06-04 08:32:52| 人1,160| 回0 | 上一篇 | 下一篇

[UVA][math] 911 - Multinomial Coefficients

0 收藏 0 0 站台

Problem G

Multinomial Coefficients

One of the first formulas we were taught in high school mathematics is MATH. Later on, we learned that this is a special case of the expansion $(a+b)^{n}$, in which the coefficient of $a^{k}b^{n-k}$ is the number of combinations of $n$ things taken $k$ at a time. We never learned (at least I never did...) what happens if instead of a binomial $a+b$ we have a multinomial $a+b+c+ldots +x$.

Problem

Your task is to write a program that, given a multinomial MATH, $kgeqslant 1$, computes the coefficient of a given term in the expansion of $m^{n}$, $ngeqslant 1$. The given term is specified by a sequence of $k$ integer numbers $z_{1}$, $z_{2}$, $ldots $, $z_{k}$, representing the powers of $a_{1}$, $a_{2}$, $ldots $, $a_{k}$ in the expansion. Note that MATH. For example, the coefficient of $ab^{2}c$ in $(a+b+c)^{4}$ is $12$.

Input

The input file contains several test cases, each of them with three lines. The first line contains a number representing the value of $n$. The second line contains a number representing the value of $k$. The third line contains $k$ numbers, representing the values of $z_{1}$, $z_{2}$, $ldots $,$z_{k}$. All test cases are such that $kleqslant 100$ and the computed coefficient is less than $2^{31}$.

Output

For each test case, write to the output one line. This line contains one integer number representing the value of the coefficient of the term MATH in the expansion of MATH.

Sample Input 1

4
3
1 2 1

Sample Output 1

12

Sample Input 2

7
4
2 3 0 2

Sample Output 2

210

Pedro Guerreiro, MIUP'2003
(Portuguese National ACM Programming Contest)

不能使用 N! / (x1!x2!...xn!), x1+x2+x3+ ... + xn = n
用列表示,然後慢慢消去。

#include <stdio.h>

int gcd(int x, int y) {
    int t;
    while(x%y)
        t = x, x = y, y = t%y;
    return y;
}
int main() {
    int i, j, k;
    int n, m, x;
    while(scanf("%d %d", &n, &m) == 2) {
        int A[105], sum = 0;
        for(i = 1; i <= n; i++)
            A[i] = i;
        while(m--) {
            scanf("%d", &x);
            sum += x;
            for(i = 2; i <= x; i++) {
                j = i;
                for(k = 2; k <= n; k++) {
                    int g = gcd(A[k], j);
                    A[k] /= g;
                    j /= g;
                }
            }
        }
        long long ret = 1;
        if(sum != n) {ret = 0;}
        else {
            for(i = 1; i <= n; i++)
                ret *= A[i];
        }
        printf("%lld\n", ret);
    }
    return 0;
}

台: Morris
人(1,160) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][time] 12136 - Schedule of a Married Man
此分上一篇:[UVA][最短路] 11573 - Ocean Currents

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能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