2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

时间:2022-01-21 12:32:57

hannnnah_j’s Biological Test

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 802    Accepted Submission(s): 269

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
题意:一个大小为 nn 的环,选 mm 个位置涂黑,要求相邻两个黑点之间至少间隔 kk 个白点,问方案数。
题解:n-m个座位分成m份,每份不小于k,也就是剩余n-m-m*k个相同的球放到m个不同的盒子里公式为C(n-m*k-m, m);
nn个相同的球放到mm个不同的盒子里公式是C(nn+mm-1, mm-1);
所以代入nn=n-m-m*k , mm=m;
C(n - m -m*k + m -1 , m-1) = C(n - m*k -1,m - 1)

ans = n * C(n - m*k -1,m - 1)/m;

乘n相当于第一个人有n种选择,而学生在这里都是一样的,所以要是重复计算了m次,最后除以m。

逆元 费马小处理

 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define ll long long
#define mod 1000000007
#define PI acos(-1.0)
#define N 1000000000
using namespace std;
ll quickmod(ll a,ll b)
{
ll sum=;
while(b)
{
if(b&)
sum=(sum*a)%mod;
b>>=;
a=(a*a)%mod;
}
return sum;
}
ll combine1(ll n,ll m) //计算组合数C(n,m)
{
if(n<||m<)
return ;
ll sum=; //线性计算
for(ll i=,j=n;i<=m;i++,j--)
sum=(((sum*j)%mod)*quickmod(i,mod-))%mod;
return sum;
}
int t;
ll nn,mm,kk;
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=;i<=t;i++)
{
scanf("%I64d %I64d %I64d",&nn,&mm,&kk);
if(mm==)
printf("%I64d\n",nn);
else
printf("%I64d\n",(((combine1(nn-mm*kk-,mm-)%mod)*quickmod(mm,mod-))%mod*nn)%mod);
}
}
return ;
}