andy6804tw
8/3/2016 - 7:49 AM

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=30526 這題要注意可能會溢位必須要用BigInteger,有大小寫區分 ex: 排列組合abbccc =>6!/(1!*2!*3!)

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=30526

這題要注意可能會溢位必須要用BigInteger,有大小寫區分 ex: 排列組合abbccc =>6!/(1!2!3!)

package ITSA;

import java.math.BigInteger;
import java.util.*;

public class Test {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int test=Integer.parseInt(sc.nextLine());
		for(int j=0;j<test;++j)
		{
			String in=sc.nextLine();
			String[] split=in.split(" ");
			char[] arr=split[1].toCharArray();
			int[] count=new int[52];
			BigInteger bi=new BigInteger("1");
			for(int i=1;i<=arr.length;++i)
			{
				bi=bi.multiply(new BigInteger(Integer.toString(i)));
				if(arr[i-1]>='a' && arr[i-1]<='z')
					count[arr[i-1]-'a']++;
				if(arr[i-1]>='A' && arr[i-1]<='Z')
					count[arr[i-1]-'A'+26]++;
			}
			for(int i=0;i<count.length;++i)
			{
				if(count[i]>1)
				{
					for(int k=count[i];k>0;--k)
						bi=bi.divide(new BigInteger(Integer.toString(k)));	
				}
			}
			System.out.println(bi.mod(new BigInteger(split[0])));	
		}
	}
	/* 
    題目:2016 闖關組 [Problem B10] Password Complexity
    作者:1010
    時間:西元 2016 年 8 月 */
}