hannnnah_j’s Biological Test 组合数学 Lucas定理

hannnnah_j’s Biological Test
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1059 Accepted Submission(s): 367

Problem Description
hannnnah_j is a teacher in WL High school who teaches biology.

One day, she wants to test m students, thus she arranges n different seats around a round table.

In order to prevent cheating, she thinks that there should be at least k empty seats between every two students.

hannnnah_j is poor at math, and she wants to know the sum of the solutions.So she turns to you for help.Can you help her? The answer maybe large, and you need to mod 1e9+7.

Input
First line is an integer T(T≤1000).
The next T lines were given n, m, k, respectively.
0 < m < n < 1e6, 0 < k < 1000

Output
For each test case the output is only one integer number ans in a line.

Sample Input
2
4 2 6
5 2 1

Sample Output
0
5

Source
2016 ACM/ICPC Asia Regional Shenyang Online

题意:
现在 m个考生人需要坐在有n个座位的圆桌上。你需要安排位置,使得任意两个考生之间相距至少k个位置。桌子有编号,考生a和b交换位置视作一种方案,问有多少方案,mod 1e9+7。(0 < m < n < 1e6, 0 < k < 1000)

解法:
首先必须有n>=m+k*m,否则答案是0。

现在先考虑m个人的相对关系(即不考虑座位编号和人对应,先只考虑人与人间的间隔,即位置分配),
先在n个座位中抽走k*m个,每个人和k个座位绑在一起,一旦这个人有了座位,他后面自动添加这k个座位。
现在因为是确定相对关系,那么第一个人必有一个座位,那么剩下n-1-k*m个座位,在这之中选m-1个作为给其余人座,即C(n-1-k*m,m-1),现在相对位置就形成了。下面开始对号入座。

由于这第一个人有n中选择,选择[1,n]这n个座位编号,所以将答案先乘以n,即n*C(n-1-k*m,m-1)。不过这样做绝对比真实答案要大,因为是个环形,如果第一个人坐到靠后的位置,那么后面的人就会坐到编号较小的位置,所以就算的有重复,因为当初相对位置为第一的人实际不是总是对应最小编号。

想想 ,已经计算中的所有方法中,相对位置中的每个人实际成为其座位编号最小的人的机会是相等的,故将答案除以m,即n*C(n-1-k*m,m-1)/m。

另 :对于m=1,特判,因为1个人没有间隔。

AC代码:

#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值