其他分享
首页 > 其他分享> > LightOJ - 1067 Combinations

LightOJ - 1067 Combinations

作者:互联网

 

题目描述:

Given n different objects, you want to take k of them. How many ways to can do it?

For example, say there are 4 items; you want to take 2 of them. So, you can do it 6 ways.

Take 1, 2

Take 1, 3

Take 1, 4

Take 2, 3

Take 2, 4

Take 3, 4

Input

Input starts with an integer T (≤ 2000), denoting the number of test cases.

Each test case contains two integers n (1 ≤ n ≤ 106), k (0 ≤ k ≤ n).

Output

For each case, output the case number and the desired value. Since the result can be very large, you have to print the result modulo 1000003.

Sample Input

3

4 2

5 0

6 4

Sample Output 

Case 1: 6

Case 2: 1

Case 3: 15

题目大意:

从n个数当中取k个数,问有多少种取法?

解题报告:

1:先用dic数组将阶乘打表。

2:根据公式计算时,除法要转为乘上逆元。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000003;
const ll N = 1e6+10;
ll dic[N];
ll exgcd(ll a, ll b, ll& x, ll& y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }
    ll t = exgcd(b, a%b, y, x);
    y -=a/b*x;
    return t;
}
ll inv(ll a, ll mod){ // 求在mod下a的逆元
    ll x, y;
    exgcd(a, mod, x, y);
    return (x%mod+mod)%mod;
}
void init(){
    dic[0] = 1;
    for(ll i=1; i<N; ++i){
        dic[i] = dic[i-1]*i, dic[i] %= mod;
    }
}
int main(){
    ll t, cas = 1, n, k;
    scanf("%lld", &t);
    init();
    while(t--){
        scanf("%lld%lld", &n, &k);
        printf("Case %lld: %lld\n", cas++, (dic[n]*inv(dic[k], mod)%mod)*inv(dic[n-k], mod)%mod );
    }
    return 0;
}

 

baronLJ 发布了145 篇原创文章 · 获赞 4 · 访问量 1万+ 私信 关注

标签:return,ll,LightOJ,exgcd,1067,dic,Take,Combinations,mod
来源: https://blog.csdn.net/jun_____/article/details/104070272