字串数
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2142 Accepted Submission(s): 458
Problem Description
一个A和两个B一共可以组成三种字符串:"ABB","BAB","BBA". 给定若干字母和它们相应的个数,计算一共可以组成多少个不同的字符串.
Input
每组测试数据分两行,第一行为n(1<=n<=26),表示不同字母的个数,第二行为n个数A1,A2,...,An(1<=Ai<=12),表示每种字母的个数.测试数据以n=0为结束.
Output
对于每一组测试数据,输出一个m,表示一共有多少种字符串.
Sample Input
2
1 2
3
2 2 2
0
Sample Output
3
90
Source
Recommend
JGShining
/* 2 设多重集的个数n = n1 + n2 + ... + nk; 3 则该多重集的排列个数为:n!/(n1!n2!...nk!). 4 */
import java.util.*; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); int n; int sum; int[] data = new int[27]; BigInteger a,b; while(cin.hasNext()){ n = cin.nextInt(); if(n==0) break; sum = 0; for(int i=0; i<n; i++){ data[i] = cin.nextInt(); sum += data[i]; } a = BigInteger.valueOf(1); for(int i=2; i<=sum; i++) a = a.multiply(BigInteger.valueOf(i)); for(int i=0; i<n; i++){ b = BigInteger.valueOf(1); for(int j=2; j<=data[i]; j++) b = b.multiply(BigInteger.valueOf(j)); a = a.divide(b); } System.out.println(a); } } }