HDU 5894-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): 1097    Accepted Submission(s): 383


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
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5906  5905  5904  5903  5902 
 

题目意思:

有n 个座位,m 个人, 围坐成一个圆,要求任意两个人之间至少空k 个座位,求对1e9取模后的方案数

解题思路:

共n 个座位,先放m 个人,至少空k 个座位,所以剩下 n - m - m*k个座位。
下面就是“隔板法”,类似于求(n-m-m*k个球,放在m个不同的箱子里(可以为空)的方案数。
/**********************************/
对于求n球,放在m个不同的箱子里(可以为空)的方案数是:C(m+n-1,m-1)或者C(m+n-1,n)。
思路如下:将n个物品分成m组,需要用(m-1)个隔板,但是可能为空,所以人为增加(m-1)个物品和n个物品放在一起成一排,然后变成从(m-1+n)个空中选出(m-1)个空放隔板,所以方案数可得如上。
/**********************************/
第一个学生有n 种选择,除m去掉重复的情况,所以得到答案是:C(n-m-m*k+m-1,m-1) *n/m。

#include <iostream>
#include <cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
/*大组合数模板C(n,m)%p*/
typedef long long LL;
const LL p=1e9+7;
LL pow_mod(LL a, LL b)
{
    LL res = 1;
    while(b != 0)
    {
        if(b&1) res = (res * a) % p;
        a = (a*a) % p;
        b >>= 1;
    }
    return res;
}

LL Comb(LL a, LL b)
{
    if(a < b)   return 0;
    if(a == b)  return 1;
    if(b > a - b)   b = a - b;

    LL ans = 1, ca = 1, cb = 1;
    for(LL i = 0; i < b; ++i)
    {
        ca = (ca * (a - i))%p;
        cb = (cb * (b - i))%p;
    }
    ans = (ca*pow_mod(cb, p - 2)) % p;
    return ans;
}

LL Lucas(LL n, LL m)
{
    LL ans = 1;

    while(n&&m&&ans)
    {
        ans = (ans*Comb(n%p, m%p)) % p;
        n /= p;
        m /= p;
    }
    return ans;
}
/*******************************************/
int main()
{
    int t,k;
    scanf("%d",&t);
    while(t--)
    {
        LL n,m;
        scanf("%I64d%I64d%d",&n,&m,&k);
        if(m==1)
        {
            printf("%I64d\n",n);
            continue;
        }
        if (n<m+m*k)
        {
            puts("0");
            continue;
        }
        LL a = Lucas(n-m*k-1,m-1);
        LL b = pow_mod(m,p-2)%p;
        printf("%I64d\n",((a*n)%p*b)%p);
    }
    return 0;
}
/*
2
4 2 6
5 2 1
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值