Gym 101147G - The Galactic Olympics

題意:
T組數據,每組數據兩個數n(運動項目數量),k(學生數量),每個運動只能有一個學生,學生可以參加多個運動項目.求問有多少種分配方法.


思路:
1.n <= k時,因爲要滿足題意,那麼答案就是A(k,n) .
2.n > k時,問題即可以轉換爲n個可區分的小球放入k個可區分的盒子裏,盒子不爲空,那麼答案就是k!S(n,k) . (S表示的第二類斯特林數)


代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAX = 1e6 + 5;
const int MAXN = 1e3 + 5;
LL cnt[MAX],s[MAXN][MAXN];

void Init() {
  cnt[0] = 1;
  for (int i = 1; i <= 1e3; ++i) {
    cnt [i] = cnt[i-1] * i % MOD;
  }
  for(int i = 1; i < MAXN; ++i) {
    for (int j = 1; j <= i; ++j) {
      if (i == j || j == 1) s[i][j] = 1;
      else if (i > 1) 
      s[i][j] = (j * s[i-1][j] % MOD + s[i-1][j-1]) % MOD;
    }
  }
}

int main() {
  int t,n,k;
  freopen("galactic.in","r",stdin);
  scanf("%d",&t);
  Init();
  while (t--) {
    LL ans = 1;
    scanf("%d%d",&n,&k);
    if (n <= k) {
      for (LL i = n; i >= n - k + 1; --i) {
        ans = (ans * i) % MOD;
      }
      printf("%lld\n",ans);
    }
    else {
      printf("%lld\n",cnt[k] * s[n][k] % MOD);
    }
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章