首页 > 其他 > 详细

LightOJ - 1067 - Combinations(组合数)

时间:2019-11-19 00:49:37      阅读:98      评论:0      收藏:0      [点我收藏+]

链接:

https://vjudge.net/problem/LightOJ-1067

题意:

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

思路:

组合数。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<utility>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 1e6+10;
const int MOD = 1e6+3;

int n, k;
int P[MAXN];

LL PowMod(int a, int b)
{
    LL res = 1;
    while(b)
    {
        if (b&1)
            res = (1LL*res*a)%MOD;
        a = (1LL*a*a)%MOD;
        b >>= 1;
    }
    return res;
}

void Init()
{
    P[1] = 1;
    for (int i = 2;i < MAXN;i++)
        P[i] = 1LL*i*P[i-1]%MOD;
}

int main()
{
    Init();
    int cnt = 0;
    int t;
    scanf("%d", &t);
    while(t--)
    {
        printf("Case %d:", ++cnt);
        scanf("%d%d", &n, &k);
        if (k == 0 || n == k)
        {
            puts(" 1");
            continue;
        }
        int tmp = 1LL*P[k]*P[n-k]%MOD;
        tmp = PowMod(tmp, MOD-2);
        printf(" %lld\n", 1LL*P[n]*tmp%MOD);
    }
    
    return 0;
}

LightOJ - 1067 - Combinations(组合数)

原文:https://www.cnblogs.com/YDDDD/p/11886520.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!