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;
}

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

时间: 2024-10-01 17:10:41

LightOJ - 1067 - Combinations(组合数)的相关文章

LightOJ 1067 Combinations

求 \(C_n^k\%p\),\(C\) 为组合数,\(p=1000003\),是个质数 前置芝士: 费马小定理 \(a^{p-1} \equiv a \mod p\) , \(p\) 为质数(费马小定理) 二项式定理: \[ (a+b)^n=\sum_{i=0}^n{C_n^i}*a^i*b^{n-i} \] 当然,因为组合数是对称的,所以也可以写成 \[ (a+b)^n=\sum_{i=0}^n{C_n^i}*a^{n-i}*b^i \] 柿子1 \((a+b)^p \equiv a^p+b

LightOJ 1067 组合数取模

Description 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

(每日算法)LeetCode --- Combinations (组合数)

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] //这是一个数学组合问题,即从n个数中随机选出k个数.可以用递归回溯来实现 <pre>//1 递归一次,填入一个数字 //2 填入

Light OJ 1067 Combinations (乘法逆元)

Description 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

lightoj 1095 - Arrange the Numbers(dp+组合数)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题解:其实是一道简单的组合数只要推导一下错排就行了.在这里就推导一下错排 dp[i]=(i-1)*dp[i-2](表示新加的那个数放到i-1中的某一个位置然后那个被放位置的数放在i这个位置就是i-2的错排)+(i-1)*dp[i-1](表示新加的那个数放到i-1中的某一个位置然后用那个位置被占的数代替i这个位置的数就是i-1的错排) #include <iostream

lightoj 1060 - nth Permutation(组合数+贪心)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1060 题解:如果是不重复数的这些操作可以用康托展开的逆来求,如果是有重复数字出现康托展开的逆就要稍微变一下.要除去自身个数的组合数具体看一代码,暴力就行 #include <iostream> #include <cstring> #include <cstdio> using namespace std; typedef long long ll;

LightOJ - 1246 Colorful Board(DP+组合数)

http://lightoj.com/volume_showproblem.php?problem=1246 题意 有个(M+1)*(N+1)的棋盘,用k种颜色给它涂色,要求曼哈顿距离为奇数的格子之间不能涂相同的颜色,每个格子都必须有颜色,问可行的方案数. 分析 经一波分析,根据曼哈顿距离为奇数这一信息,可以将棋盘分为两部分,也就是相邻格子不能有相同颜色.一种颜色只能在一个部分中出现.现在考虑对一个部分的格子操作, dp[i][j]表示i个格子选择用了j种颜色的方案数,于是可以得到这样的递推式:

light oj 1067 组合数取模

题目连接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26784 题目: Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description: Given n different objects, you want to take k of them. How many ways to can do it? F

LightOJ - 1005 - Rooks(组合数)

链接: https://vjudge.net/problem/LightOJ-1005 题意: A rook is a piece used in the game of chess which is played on a board of square grids. A rook can only move vertically or horizontally from its current position and two rooks attack each other if one i