字串数

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2661    Accepted Submission(s): 606

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   |   We have carefully selected several similar problems for you:  
1263 
1265 
1262 
1260 
1258 

/*
* (a1+a2+...)!/(a1!*a2!*...)
*/
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Scanner; public class Main {//把大数用到极致了
public static void main(String[] args) {
Scanner input=new Scanner(new InputStreamReader(System.in));
int n;
while((n=input.nextInt())!=0){
int a[]=new int[n];
long sum=0;
for(int i=0;i<n;i++){
a[i]=input.nextInt();
sum+=a[i];
}
BigInteger a1=NUM(sum);
BigInteger s=BigInteger.ONE;
for(int i=0;i<n;i++){
s=s.multiply(NUM(a[i]));
}
// System.out.println(a1+" "+s);
System.out.println(a1.divide(s));
}
} private static BigInteger NUM(long sum) {//计算阶乘
BigInteger sum1=BigInteger.ONE;
for(int i=2;i<=sum;i++)
sum1=sum1.multiply(BigInteger.valueOf(i));
return sum1;
}
}

05-08 15:10