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.
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
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
题目意思:
有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
*/