二十八 211. 计算系数 (组合计数|逆元)

211. 计算系数 (组合计数|逆元)
image

数论之快速幂、扩欧算法、同余与逆元

组合计数

import java.util.*;

public class Main {
    private static final int mod = 10007;
    private static int[][] C = new int[1010][1010];
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int k = sc.nextInt();
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        a %= mod;
        b %= mod;
        
        for (int i = 0; i <= k; 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;
                }
            }
        }
        int res = C[k][n];
        for (int i = 0; i < n; i++) {
            res = res * a % mod;
        }
        for (int i = 0; i < m; i++) {
            res = res * b % mod;
        }
        
        System.out.println(res);
    }
}

逆元

import java.util.*;

public class Main {
    private static final int mod = 10007;
    
    private static int fastPow(int a, int k) {
        a %= mod;
        int res = 1;
        while (k != 0) {
            if ((k & 1) != 0) {
                res = res * a % mod;
            }
            a = a * a % mod;
            k >>= 1;
        }
        return res;
    }
    
    private static int C(int n, int m) {
        int res = 1;
        for(int i = 1, j = n; i <= m; i++, j--) {
            res = res * j % mod * fastPow(i, mod - 2) % mod;
        }
        return res;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int k = sc.nextInt();
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int res = C(k, n) * fastPow(a, n) % mod * fastPow(b, m) % mod;
        System.out.println(res);
    }
}
posted @   he0707  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示