ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LightOJ - 1067 Combinations

2020-01-22 15:42:36  阅读:340  来源: 互联网

标签:return ll LightOJ exgcd 1067 dic Take Combinations mod


 

题目描述:

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有