JAVA大数类练手

来源:互联网 发布:mac电脑pdf如何编辑 编辑:程序博客网 时间:2024/05/22 01:30

package buct_三国杀;

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

public class Main
{

    public static BigInteger C(int x, int n)
    {
        BigInteger ans = BigInteger.ONE;
        int i, j;
        for (i = n; i >= n - x + 1; i--)
        {
            ans = ans.multiply(BigInteger.valueOf(i));
        }
        for (i = x; i >= 1; i--)
        {
            ans = ans.divide(BigInteger.valueOf(i));
        }
        return ans;
    }

    public static void main(String[] args)
    {
        BigInteger ans;
        Scanner cin = new Scanner(System.in);
        int t, n, x, k, u, i;
        t = cin.nextInt();
        for (u = 1; u <= t; u++)
        {
            n = cin.nextInt();
            x = cin.nextInt();
            k = n / x;
            ans = BigInteger.ONE;
            for (i = 0; i < k; i++)
            {
                ans = ans.multiply(C(x, n - x * i));
            }
            for (i = 1; i <= k; i++)
            {
                ans = ans.divide(BigInteger.valueOf(i));
            }
            System.out.println(ans);
        }
    }
}

原创粉丝点击