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
思路:感觉跟阶乘有关(sum!/(a[1]!*……*a[n]!)),就莽了一下,发现数据很大,就改为JAVA水过
Java代码:
import java.util.Scanner;
import java.io.BufferedInputStream;
import java.math.*;
public class Main {
static int maxn = 1000000;
static int a[] = new int[maxn];
public static BigInteger jie(int n) {
BigInteger ans = BigInteger.ONE;
for (int i = 1; i <= n; i++)
ans = ans.multiply(BigInteger.valueOf(i));
return ans;
}
public static void main(String[] agrs) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
while (cin.hasNext()) {
int n, sum;
BigInteger ans, res;
n = cin.nextInt();
if (n == 0)
break;
res = BigInteger.ONE;
ans = BigInteger.ONE;
sum = 0;
for (int i = 0; i < n; i++) {
a[i] = cin.nextInt();
sum += a[i];
res=res.multiply(jie(a[i]));
}
System.out.println(jie(sum).divide(res));
}
}
}