hannnnah_j’s Biological Test (lucas定理 )

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

思路:lucas定理

题意:

现在 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个人没有间隔。


#include<stdio.h>
#include<string.h>
#define mod 1000000007
using namespace std;
typedef long long ll;
ll n,m,k;
ll qpow(ll x,int n){ //快速幂 
	ll ans=1;
	while(n){
		if(n%2) ans=ans*x%mod;
		x=x*x%mod;
		n/=2;
	}
	return ans;
}
ll C(int n,int m){ //lucas定理 
	if(m>n) return 0;
	ll ans=1;
	for(int i=1;i<=m;i++)
	ans=ans*((n+i-m)*qpow(i,mod-2)%mod)%mod;
	return ans;
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d",&n,&m,&k);
		if(m==1) printf("%lld\n",n);
		else printf("%lld\n",(C(n-k*m-1,m-1)*n%mod)*qpow(m,mod-2)%mod);
	}
	return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值