886. 求组合数 II

886. 求组合数 II

题目

给定 n 组询问,每组询问给定两个整数 a,b,请你输出 Cbamod(109+7) 的值。

输入格式
第一行包含整数 n。

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

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

数据范围
1≤n≤10000,
1≤b≤a≤105
输入样例:
3
3 1
5 3
2 2
输出样例:
3
10
1

代码

#include <iostream>

using namespace std;

typedef long long LL;
const int N = 100010, mod = 1e9+7;
int fact[N], infact[N];


// 快速幂
int qmi(int a, int b, int p){
    int res = 1;
    while(b){
        if(b & 1) res = (LL) res * a % p;
        a = (LL) a * a % p;
        b >>= 1;
    }
    return res;
}

int main()
{
    int n;
    cin >> n;
    
    // 预处理出阶乘数组fact[]与逆元阶乘数组infact[]
    // C(a,b) = a! / (b! * (a-b)!) == a! * (b!)^-1 * ((a-b)!)^-1
    // 逆元的阶乘:(b!)^-1 = ((b-1)!)^-1 * b^-1
    // 费马小定理:如果a与p互质,并且a不是p的倍数,a*a^-1 = 1
    // 那么  a^(p-1) = 1 (mod p) => a*a^(p-2) = 1 (mod p) => a模p的逆元a^-1是a^(p-2)  
    fact[0] = infact[0] = 1;
    for(int i = 1; i < N; i++){
        fact[i] = (LL)fact[i-1] * i % mod;
        infact[i] = (LL)infact[i-1] * qmi(i, mod-2, mod) % mod;
    }
    
    while (n --){
        int a, b;
        cin >> a >> b;
        
        printf("%d\n", (LL)fact[a] * infact[b] % mod * infact[a-b] % mod);
    }
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,有多种方法可以解组合数。下面我将介绍几种常见的方法: 1.编写函数计算组合数:可以根据给定的公式编写一个函数来计算组合数。例如,可以使用一个循环计算组合数的值。具体的代码如下: ```python def Combinatorial(n, i): # n>=i Min = min(i, n - i) result = 1 for j in range(0, Min): result = result * (n - j) / (Min - j) return result if __name__ == '__main__': print(int(Combinatorial(45, 2))) ``` 2.使用第三方模块scipy计算排列组合的具体数值:可以使用scipy库中的comb函数来计算组合数。具体的代码如下: ```python from scipy.special import comb C = comb(45, 2) print(C) ``` 3.使用阶乘的方式合数:可以利用阶乘函数来计算组合数。具体的代码如下: ```python import math def factorial_me(n): result = 1 for i in range(2, n + 1): result = result * i return result def comb_1(n, m): return math.factorial(n) // (math.factorial(n - m) * math.factorial(m)) def comb_2(n, m): return factorial_me(n) // (factorial_me(n - m) * factorial_me(m)) if __name__ == '__main__': print(comb_1(45, 2)) print(comb_2(45, 2)) ``` 4.使用itertools列出排列组合的全部情况:可以使用itertools库中的combinations和permutations函数来列出所有的组合情况。具体的代码如下: ```python from itertools import combinations, permutations # 列举排列结果 print(list(permutations([1, 2, 3], 2))) # 列举组合结果 print(list(combinations([1, 2, 3], 2))) ``` 综上所述,以上是几种常见的Python解组合数的方法。你可以根据需选择合适的方法来使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python计算排列组合数](https://blog.csdn.net/hitzijiyingcai/article/details/107021744)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Python快速合数C(n,m)三种方法整理](https://blog.csdn.net/bianxia123456/article/details/105151104)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值