码迷,mamicode.com
首页 > 其他好文 > 详细

LightOJ - 1067 - Combinations(组合数)

时间:2019-11-19 01:05:38      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:stream   组合数   ems   name   const   using   you   tps   long   

链接:

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(组合数)

标签:stream   组合数   ems   name   const   using   you   tps   long   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!