算法打卡之组合数

例题1:求组合数 I

给定 n 组询问,每组询问给定两个整数 a,b,请你输出 在这里插入图片描述
的值。

输入格式

第一行包含整数 n。

接下来 n 行,每行包含一组 a 和 b。

输出格式

共 n 行,每行输出一个询问的解。

数据范围

1≤n≤10000,
1≤b≤a≤2000

输入样例:
3
3 1
5 3
2 2
输出样例:
3
10
1
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    static final int N = 2010, mod = (int) (1e9 + 7);
    static int[][] c = new int[N][N];

    public static void init() {
        for (int i = 0; i < N; i++)
            for (int j = 0; j <= i; j++)
                if (j == 0) c[i][j] = 1;
                else c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
    }

    public static void main(String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        init();
        for (int i = 0; i < n; i++) {
            str = bufferedReader.readLine().split(" ");
            int a = Integer.parseInt(str[0]);
            int b = Integer.parseInt(str[1]);
            System.out.println(c[a][b]);
        }
    }
}
例题2:求组合数 II

给定 n 组询问,每组询问给定两个整数 a,b,请你输出 在这里插入图片描述
的值。

输入格式

第一行包含整数 n。

接下来 n 行,每行包含一组 a 和 b。

输出格式

共 n 行,每行输出一个询问的解。

数据范围

1≤n≤10000,
1≤b≤a≤ 1 0 5 10^5 105

输入样例:
3
3 1
5 3
2 2
输出样例:
3
10
1
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static int N = 100010, mod = 1000000007;
    public static long[] fact, infact;

    public static long qmi(long a, long b, long p) {
        long res = 1;
        while (b > 0) {
            if ((b & 1) > 0) res = res * a % p;
            b = b >> 1;
            a = a * a % p;
        }
        return res;
    }

    public static void main(String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        fact = new long[N];
        infact = new long[N];
        fact[0] = infact[0] = 1;
        for (int i = 1; i < N; i++) {
            fact[i] = fact[i - 1] * i % mod;        //阶乘
            infact[i] = infact[i - 1] * qmi(i, mod - 2, mod) % mod;     //快速幂逆元
        }
        for (int i = 0; i < n; i++) {
            str = bufferedReader.readLine().split(" ");
            int a = Integer.parseInt(str[0]);
            int b = Integer.parseInt(str[1]);
            System.out.println(fact[a] * infact[b] % mod * infact[a - b] % mod);
        }
    }
}
例题3:求组合数 III

给定 n 组询问,每组询问给定三个整数 a,b,p,其中 p 是质数,请你输出 在这里插入图片描述
的值。

输入格式

第一行包含整数 n。

接下来 n 行,每行包含一组 a,b,p。

输出格式

共 n 行,每行输出一个询问的解。

数据范围

1≤n≤20,
1≤b≤a≤ 1 0 18 10^{18} 1018,
1≤p≤ 1 0 5 10^5 105,

输入样例:
3
5 3 7
3 1 5
6 4 13
输出样例:
3
3
2
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static long p;

    public static long qmi(long a, long k) {
        long res = 1;
        while (k > 0) {
            if ((k & 1) > 0) res = (res * a) % p;
            k = k >> 1;
            a = (a * a) % p;
        }
        return res;
    }

    public static long C(long a, long b) {
        long res = 1;
        for (long i = 1, j = a; i <= b; i++, j--) {
            res = ((res * j) % p);
            res = (res * qmi(i, p - 2)) % p;
        }
        return res;
    }

    public static long lucas(long a, long b) {
        if (a < p && b < p) return C(a, b);
        return C(a % p, b % p) * lucas(a / p, b / p) % p;
    }

    public static void main(String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        //卢卡斯定理
        for (int i = 0; i < n; i++) {
            str = bufferedReader.readLine().split(" ");
            long a = Long.parseLong(str[0]);
            long b = Long.parseLong(str[1]);
            p = Long.parseLong(str[2]);
            System.out.println(lucas(a, b));
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I'm 程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值