Gym 101147G 第二类斯特林数

大致题意:

n个孩子,k场比赛,每个孩子至少参加一场比赛,且每场比赛只能由一个孩子参加。问有多少种分配方式。

分析:

k>n,就无法分配了。

k<=n。把n分成k堆的方案数乘以n的阶乘。N分成k堆得方案数即第二类斯特林数

http://blog.csdn.net/acdreamers/article/details/8521134

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll mod=1e9+7;
const int maxn=1005;
ll s[maxn][maxn];

void init()
{
    s[0][0]=1;
    for(int i=1;i<=1000;i++)
        for(int j=1;j<=i;j++)
            s[i][j]=(j*s[i-1][j]+s[i-1][j-1])%mod;
    for(int i=1;i<=1000;i++)
    {
        ll tmp=1;
        for(int j=1;j<=i;j++)
        {
            tmp=(tmp*j)%mod;
            s[i][j]=(s[i][j]*tmp)%mod;
        }
    }
}

int main()
{
    freopen("galactic.in","r",stdin);
    init();
    int n,k,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        if(k>n)
        {
            puts("0");
            continue;
        }
        printf("%I64d\n",s[n][k]);
    }
    return 0;
}
时间: 2024-08-11 01:35:43

Gym 101147G 第二类斯特林数的相关文章

Gym Gym 101147G 第二类斯特林数

题目链接:http://codeforces.com/gym/101147/problem/G 题意:n个人,去参加k个游戏,k个游戏必须非空,有多少种放法? 分析: 第二类斯特林数,划分好k个集合后乘以阶乘: 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 1010; 6 const long long MOD = 1000000000 + 7L; 7 long long stir[maxn][

Light OJ 1236 Race 第二类斯特林数

第二类斯特林数 n 匹马 分成1 2 3... n组 每一组就是相同排名 没有先后 然后组与组之间是有顺序的 在乘以组数的阶乘 #include <cstdio> #include <cstring> using namespace std; int dp[1010][1010]; int a[1010]; int main() { a[0] = 1; dp[0][0] = 1; for(int i = 1; i <= 1000; i++) { dp[i][0] = 0; d

swjtu oj Paint Box 第二类斯特林数

http://swjtuoj.cn/problem/2382/ 题目的难点在于,用k种颜色,去染n个盒子,并且一定要用完这k种颜色,并且相邻的格子不能有相同的颜色, 打了个表发现,这个数是s(n, k) * k! s(n, k)表示求第二类斯特林数. 那么关键是怎么快速求第二类斯特林数. 这里提供一种O(k)的算法. 第二类斯特林数: #include <cstdio> #include <cstdlib> #include <cstring> #include <

hdu 2512 一卡通大冒险(第二类斯特林数)

递推思路如下,i张卡片分成j堆,那么分为两种情况:第i张卡片自成一堆或没有自成一堆. 那么自成一堆的话就是dp[i-1][j-1]种情况 不自成一堆的话就是就能在j堆种任意挑一堆放入,所以有dp[i-1][j]*j种情况 综上,如下: dp[i][j]=dp[i-1][j]*j+dp[i-1][j-1]. 关于第二类斯特林数,百度就好. 具体代码 #include <iostream> using namespace std; int dp[2005][2005]; int main() {

poj 3088 组合计数,第二类斯特林数

题意:给出n个数字[1,n],问你可以组成多少种不同的序列,其中每一个序列由几个部分组成,每个部分包含几个数字,每部分内数字无序,部分之间数字有序.每个数字最多使用一次,可以不用. 思路:枚举从n个数字中选出i个数字(组合数),再枚举将这i个数字分成j个部分(第二类斯特林数),然后乘上j的全排列. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5

LightOJ 1326 - Race(第二类斯特林数啊 )

题目链接:http://lightoj.com/volume_showproblem.php?problem=1326 百度百科:斯特林数 ACdreamer:第一类Stirling数和第二类Stirling Disky and Sooma, two of the biggest mega minds of Bangladesh went to a far country. They ate, coded and wandered around, even in their holidays.

poj 1430 第二类斯特林数

1 #include <iostream> 2 #include <cmath> 3 #include <algorithm> 4 using namespace std; 5 6 int get2(long long n){ 7 if(n==0) 8 return 0; 9 int cnt =0; 10 while(n){ 11 cnt += n/2; 12 n = n/2; 13 } 14 return cnt; 15 } 16 int main(){ 17 18

HDU 4045 Machine scheduling (第二类斯特林数+DP)

A Baidu's engineer needs to analyze and process large amount of data on machines every day. The machines are labeled from 1 to n. On each day, the engineer chooses r machines to process data. He allocates the r machines to no more than m groups ,and

关于第二类斯特林数的一丢丢东西

关于第二类斯特林数的一丢丢东西 第二类斯特林数 S(n,m)表示有\(n\)个有区别小球,要放进\(m\)个相同盒子里,且每个盒子非空的方案数 考虑一个很容易的递推: \[S(n,m)=S(n-1,m-1)+m*S(n-1,m)\] 考虑组合意义: 假设前面的\(n-1\)个球丢进了\(m-1\)个组,因为每个组非空,所以这个球只有一种选择--自己一组 如果前面的球已经分成了\(m\)组,那么,这个球就有\(m\)种放法 所以这个递推式就是这样来的 那么,只考虑组合意义可不可以算? 当然是可以的