hannnnah_j’s Biological Test
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 235 Accepted Submission(s): 66
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
题意:给出n个不同的座位,现在让m个同学坐下,两个同学之间的座位间必须有k个空位, 问有多少种坐法,答案mod1e9+7.
题解:
首先构造出m个人,两个人旁边有k-1个座位,那问题就变成了m个正数的和是n-k*m
考虑n-k*m划分为m个正数
t | t | t | t | t | t | t | t | t | t | t | t
由于是正数,所以每个数都必须大于0,那么挡板就不能放在两边
这时候选法就是c(n-k*m-1,m-1)
n个不同的座位环形,所以起点有n种可能,ans再乘n
有m个同学,以每个人为起点算了1次,相当于多算到m次,ans除以m
ps:先判掉m=1,再判掉n<m*m+k这两种情况
然后除法用逆元
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll mo[1000005];
ll quickmod(ll a,ll k,ll m){
ll ans=1;
a%=m;
while(k){
if(k&1){
ans=ans*a%m;
}
a=a*a%m;
k>>=1;
}
return ans;
}
void init(){
mo[1]=1;
mo[0]=1;
for(ll i=2;i<=1000000;i++){
mo[i]=i*mo[i-1]%mod;
}
}
ll quickpow(ll a,ll b,ll k){
ll ans=0;
a%=k;
while(b){
if(b&1){
ans=(ans+a)%k;
}
a=(a<<1)%k;
b>>=1;
}
return ans;
}
int main(){
ll t;
init();
scanf("%lld",&t);
while(t--){
ll n,m,k;
scanf("%lld%lld%lld",&n,&m,&k);
if(m==1){
printf("%lld\n",n);
continue;
}
if(n<m+m*k){
printf("0\n");
continue;
}
ll ans=mo[n-k*m-1]*n%mod;
ll t=quickmod(((mo[m-1]*mo[n-k*m-1-m+1]%mod)*m%mod+mod)%mod,mod-2,mod);
ans=((ans*t%mod)+mod)%mod;
cout<<ans<<endl;
}
return 0;
}