LightOJ - 1067 - Combinations(组合数)

链接:

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;
}
posted @ 2019-11-19 00:08  YDDDD  阅读(157)  评论(0编辑  收藏  举报