HDU5894-hannnnah_j’s Biological Test(组合数学+Lucas定理)

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=5894

思路

首先去掉这m个人,还剩下n - m,相当于把n - m拆分成m个不小于k的数,我们可以考虑将每个数减去k - 1, 这样就转化成了将n - mk拆分成m个正数,即C(n - mk - 1, m - 1),因为是圆桌,因此乘以n,又因为每种方案实际上计算了m次,因此再除以m即可
1. 最终答案:n * C(n - mk - 1, m - 1) / m % mod
2. 然后计算这个组合数的时候应该用到Lucas定理
简单来说就是:
p是素数,那么:C(n, m) % p = C(n / p, m / p) * C(n % p, m % p) % p;
3. 除法取模要用到逆元
首先是费马小定理image(a为整数,p为素数)
那么就有a^(p - 2) ≡ 1/a (mod p)

代码

#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <bitset>
#include <algorithm>
using namespace std;

#define PI acos(-1.0)
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define mp make_pair
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "wb", stdout)
#define scan(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define sqr(x) (x) * (x)

const int maxn = 1000000 + 5;
LL a[maxn], n, m, k;
LL mod = 1e9 + 7;

void init() {
    a[0] = 1;
    for (LL i = 1; i < maxn; i++) {
        a[i] = a[i - 1] * i;
        a[i] %= mod;
    }
}

LL qpower(LL x, LL p) {
    LL ans = 1;
    x %= mod;
    while (p) {
        if (p & 1) (ans *= x) %= mod;
        (x *= x) %= mod;
        p >>= 1;
    }
    return ans;
}

LL C(int n, int m) {
    if (m > n) return 0;
    return a[n] * qpower(a[m] * a[n - m] % mod, mod - 2) % mod;
}

LL Lucas(LL n, LL m) {
    if (m == 0) return 1;
    return Lucas(n / mod, m / mod) * C(n % mod, m % mod) % mod;
}

int main() {
    int T;
    scan(T);
    init();
    while (T--) {
        scanf("%lld%lld%lld", &n, &m, &k);
        LL res = Lucas(n - m * k - 1, m - 1);
        (res *= n) %= mod;
        res *= qpower(m, mod - 2);
        res %= mod;
        printf("%lld\n", res);    
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值