题目链接:
https://vjudge.net/problem/HDU-2643
题目:
Recently in Teddy's hometown there is a competition named "Cow Year Blow Cow".N competitors had took part in this competition.The competition was so intense that the rank was changing and changing.
Now the question is:
How many different ways that n competitors can rank in a competition, allowing for the possibility of ties.
as the answer will be very large,you can just output the answer MOD 20090126.
Here are the ways when N = 2:
P1 < P2
P2 < P1
P1 = P2
Input
The first line will contain a T,then T cases followed.
each case only contain one integer N (N <= 100),indicating the number of people.
Output
One integer pey line represent the answer MOD 20090126.
Sample Input
2
2
3
Sample Output
3
13
题解:
题意:有n个数字,判断这n个数字的大小关系有多少种情况。 大小关系具有传递性,比如a<b,b<c。那么b<c.
每两个数字具有三种大小关系 a<b,a=b,a>b.
首先讲一下第二类Stirling数:
第二类Stirling数: 定义S(n,k):把n个不同的球分配到k个相同的盒子里,盒子不能为空,问有多少种方案。
我们可以根据第n个球的状态写出状态转移方程,第n个球有两种状态:
1.第n个球单独在一个盒子里,那么剩下的n-1个球分到k-1个盒子里,方案数为 S(n-1,k-1)
2.第n个球不单独在一个盒子里,也就是第n个球所在盒子里最少要有两个球。那么先把n-1个球分配到k个盒子里,然后第n个球就可以随便放到这k个盒子其中一个,那么第n个球所在盒子就最少有两个球, 对应的方案数为 k*S(n-1,k).
所以S(n,k)=S(n-1,k-1)+k*S(n-1,k). 这就是第二类Stirling数的状态转移方程。 其中S(0,0)=1,S(i,0)=0.S(i,i)=1
然后考虑这道题:
我们可以把相等的数放到一个盒子里,每个盒子里的数和其它盒子的数大小不一样,那么盒子就是不相同的,那么问题就转化成,把n个不同的数字放到k个不同的盒子(k是1到n)有多少种方案。
把n个不同数字放到k个相同盒子里的方案数是第二类Stirling数 S(n,k), 那么放到不同盒子里就是再乘上k的全排列数量,即S(n,k)*k!. k的范围是[1,n]
那么最后的答案就是 S(n,1) *1! + S(n,2)*2! + S(n,3)*3!+......S(n,n)*n!.
可以先预处理处二维数组S和阶乘。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=105;
const int mod=20090126;
ll f[maxn][maxn];
ll fac[maxn];
void init(){
fac[0]=1;
for(int i=1;i<=100;i++) fac[i]=i*fac[i-1]%mod;
f[0][0]=1;
for(int i=1;i<=100;i++){
f[i][0]=0; f[i][i]=1;
}
for(int i=2;i<=100;i++){
for(int j=1;j<i;j++){
f[i][j]=(f[i-1][j-1]+j*f[i-1][j])%mod;
}
}
}
int main(){
init();
int t,n;
cin>>t;
while(t--){
scanf("%d",&n);
ll ans=0;
for(int i=1;i<=n;i++){
ans=(ans+f[n][i]*fac[i])%mod;
}
printf("%lld\n",ans);
}
return 0;
}