首页 > 代码库 > HDU 5894 hannnnah_j’s Biological Test (组合数学)

HDU 5894 hannnnah_j’s Biological Test (组合数学)

题意:给你n 个座位,和m 个人, 安排在一个圆桌子上,要求任意两个人之间的座位至少为k 个,求方案数,答案对1e9取模。

析:一开始,我没看到是圆桌,推出一个非圆桌的,但是一换成圆桌,当时脑子就乱,先求出至少要占用多少座位,学生和空座位。

那么就剩下了 n - m - m * k 个空座位,问题就成了,假设 c = n - m - m * k,在 m 的空隙插入 c 个座位有多少种,也就是C(c+m-1, m-1)。

然后每个座位是不同的,所以每个人位置不一样再乘以 n (也就是第一个学生的位置),然后每个人 又是一样的再除以 m。

最后答案就成了 C(c+m-1, m-1) * n / m,然而交上去,并不对,要特殊判断 m 为 1的情况,答案恒为 n。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}

LL inv[maxn];
LL f[maxn], fact[maxn];

LL C(int n, int m){
  return fact[n] * f[m] % mod * f[n-m] % mod;
}

int main(){
  fact[0] = fact[1] = 1;
  f[1] = inv[1] = 1;
  for(int i = 2; i < maxn; ++i){
    inv[i] = (mod - mod/i) * inv[mod%i] % mod;
    f[i] = f[i-1] * inv[i] % mod;
    fact[i] = fact[i-1] * i % mod;
  }

  int T;  cin >> T;
  while(T--){
    int k;
    scanf("%d %d %d", &n, &m, &k);
    int c = n - m - m * k;
    if(m == 1){ printf("%d\n", n);  continue; }
    if(c < 0){ printf("0\n");  continue; }
    LL ans = C(c+m-1, m-1) * n % mod * inv[m] % mod;
    cout << ans << endl;
  }
  return 0;
}

  

HDU 5894 hannnnah_j’s Biological Test (组合数学)