hannnnah_j’s Biological Test HDU

来源:互联网 发布:淘宝小二介入买家输 编辑:程序博客网 时间:2024/06/08 14:08
 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

24 2 65 2 1

Sample Output

05

这题是一个组合数学的题,因为每个椅子是不一样的,所以其实看成链状的而不是环,我们假定链的第一个位置已经坐了人,并且顺带带走了k*m个点,而且我们把1+k个点浓缩成一个点,那么m-1个这样的点来放剩下的n-1-k*m个格子即可,然后我们可以截成n种不同的链所以要乘n,但是有因为是循环放置的,而且每个人是相同的所以通构的会有m种,因为是旋转的,所以要除m。
m等于1的时候要特判

#include<iostream>#include<cstdio>#include<cstring>#define mod 1000000007#define N 1000005using namespace std;long long fac[N];long long inv[N];long long P(long long a,long long b){    long long ans=1;    while(b)    {        if(b&1)            ans=ans*a%mod;        a=a*a%mod;        b>>=1;    }    return ans;}long long get(int n,int m){    return (fac[n]*inv[m]%mod)*inv[n-m]%mod;}void init(){    fac[0]=1;    inv[0]=1;    for(int i=1;i<N;i++)        fac[i]=fac[i-1]*i%mod;    inv[N-1]=P(fac[N-1],mod-2);    for(int i=N-2;i>=1;i--)        inv[i]=inv[i+1]*(i+1)%mod;}int main(){       init();    int n,m,k;    int t;    //cout<<get(10,5)<<endl;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&n,&m,&k);        if(m==1)            printf("%d\n",n);        else            if(n<(k+1)*m)                printf("0\n");            else            {                long long ans=get(n-1-k*m,m-1);                ans=ans*n%mod;                ans=ans*P(m,mod-2)%mod;                printf("%lld\n",ans);            }       }       return 0;}
原创粉丝点击