UVA-12316 Sewing Buttons with grandma (combination of repeating elements)

Source: Internet
Author: User

Description


Sewing Buttons with Grandma

After so many years of studying math in the Academy of Colombian mathematics (ACM) in the Tropic, Eloi have Finall Y decided to visit he grandmother for the winter of the North hemisphere. "Buttons and patches and the cold wind blowing, the days pass quickly when I am sewing" She says-eloi now remembers Ho W Grandma Quilts has love in every stitch. As a matter of fact, he has decided to learn the useful handicraft art of sewing buttons with grandma.


Eloi have realized that there was an interesting mathematical puzzle related to the task of sewing buttons to the front of a Shirt. Given a collection ofn1 buttons of color C1, n2 buttons of Color c2, ..., nk buttons of color ck, and a shirt with m fron T buttonholes, in how many ways the buttonholes can is assigned m buttons from the n1 + ... + nk buttons?

Input

The input consists of several test cases. The first line of all test case contains, integersm and K separated by a blank (1m 50 ,1 k ). Thenk lines follow each containing a integer ni with 1 ni. The last test was followed by a line containing the zeros.

Output

The output consists of one line per test case containing the number of ways them buttonholes can be assigned m buttons from the n1 + ... + nk buttons, assuming that the colors c 1,..., ck is pairwise distinct.

Sample Input

1 33113 2423) 2110 0

Sample Output

370

Test instructions: give you m button position, then have n different colors of repeated buttons, to find the number of combinations

Idea: First try to assume that f[i][j] means that I have a different color of the buttons to make up J number of possible numbers, then

F [I][J]=< Span style= "Position:absolute; Top:-2.707em; Left:0.029em ">∑ i = 1 n < Span style= "Position:absolute; Top:-2.707em; Left:0.058em ">∑ j = 1 m ∑ k=0 a[i] F [I?1][J?k]?C [J][k]which c [ j [ k Represents the number of combinations

In the case of I-1, the K is picked from J to put the first I color, so multiply the number of combinations

Import Java.math.biginteger;import Java.util.scanner;public class Main {public static void main (string[] args) {final int MAXN = 105;int a[] = new INT[MAXN]; BigInteger f[][] = new BIGINTEGER[MAXN][MAXN]; BigInteger c[][] = new Biginteger[maxn][maxn];int N, m;for (int i = 0; I <=; i++) {c[i][0] = Biginteger.one;c[i][i] = Biginteger.one;} for (int i = 2; I <=, i++) for (int j = 1; j < I; j + +) C[i][j] = C[i-1][j-1].add (C[i-1][j]);  Scanner in = new Scanner (system.in), while (true) {m = In.nextint (); n = in.nextint (); if (M = = 0 && n = = 0) break;for (int i = 1; I <= n; i++) A[i] = In.nextint (); for (int i = 0; I <=, i++) for (int j = 0; J <=; j + +) if (j = = 0  ) F[i][j] = Biginteger.one;else F[i][j] = biginteger.zero;for (int i = 1; I <= n; i++) for (int j = 1; j <= M; j + +) for (int k = 0; k <= a[i]; k++) if (j-k >= 0) F[i][j] = F[i][j].add (f[i-1][j-k].multiply (c[j][k))); System.out.println (F[n][m]);}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.