hdu 1261 字串数

时间:2023-11-12 09:58:08

hdu 1261 字串数

解题思路:ACM紫书 第十章 P319 有重复元素的全排列

答案: 所有数的和的阶乘 除以 每个数阶乘的乘积

因为给定 (26*12)! 会爆掉(long long),这里用java 的BigInteger.

import java.math.BigInteger;
import java.util.Scanner; public class Main {
public static BigInteger jc(BigInteger n) {
if (n.compareTo(new BigInteger("1")) == 0)
return new BigInteger("1");
else
return n.multiply(jc(n.subtract(new BigInteger("1"))));
} public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
if (n == 0)
break;
BigInteger[] numarr = new BigInteger[n];
BigInteger n1 = new BigInteger("0");
BigInteger sum = new BigInteger("1");
for (int i = 0; i < n; i++) {
numarr[i] = sc.nextBigInteger();
n1 = n1.add(new BigInteger("" + numarr[i]));
sum = sum.multiply(new BigInteger("" + jc(numarr[i])));
}
System.out.println("" + jc(n1).divide(sum));
}
}
}