HDU 1261 字串数

字串数

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


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
 
 
 
解析:排列组合+大数。令sum = A1+A2+...+An,易知结果为sum! /(A1!A2!...An!)。而sum可以达到(12*26)!,需要用大数来解决。
 
 
 
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static BigInteger fact(int n) {
        BigInteger ret = BigInteger.ONE;
        for(int i = 2; i <= n; ++i){
            ret = ret.multiply(BigInteger.valueOf(i));
        }
        return ret;
    }
    
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n;
        while ((n = in.nextInt()) != 0) {
            int[] num = new int[n];
            int sum = 0;
            for(int i = 0; i < n; ++i){
                num[i] = in.nextInt();
                sum += num[i];
            }
            BigInteger res = fact(sum);
            for(int i = 0; i < n; ++i){
                res = res.divide(fact(num[i]));
            }
            System.out.println(res);
        }
        in.close();
    }
}
原文地址:https://www.cnblogs.com/inmoonlight/p/5705623.html