首页 > 代码库 > lightoj1326_第二类Stirling

lightoj1326_第二类Stirling

题目链接http://lightoj.com/volume_showproblem.php?problem=1326

1051 - Good or Bad
   PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB

A string is called bad if it has 3 vowels in a row, or 5 consonants in a row, or both. A string is called good if it is not bad. You are given a string s, consisting of uppercase letters (‘A‘-‘Z‘) and question marks (‘?‘). Return "BAD" if the string is definitely bad (that means you cannot substitute letters for question marks so that the string becomes good), "GOOD" if the string is definitely good, and "MIXED" if it can be either bad or good.

The letters ‘A‘, ‘E‘, ‘I‘, ‘O‘, ‘U‘ are vowels, and all others are consonants.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case begins with a non-empty string with length no more than 50.

Output

For each case of input you have to print the case number and the result according to the description.

Sample Input

Output for Sample Input

5

FFFF?EE

HELLOWORLD

ABCDEFGHIJKLMNOPQRSTUVWXYZ

HELLO?ORLD

AAA

Case 1: BAD

Case 2: GOOD

Case 3: BAD

Case 4: MIXED

Case 5: BAD

 

第一类Stirling数 s(p,k)

    

s(p,k)的一个的组合学解释是:将p个物体排成k个非空循环排列的方法数。

 

s(p,k)的递推公式: s(p,k)=(p-1)*s(p-1,k)+s(p-1,k-1) ,1<=k<=p-1

边界条件:s(p,0)=0 ,p>=1  s(p,p)=1  ,p>=0

 

递推关系的说明:

考虑第p个物品,p可以单独构成一个非空循环排列,这样前p-1种物品构成k-1个非空循环排列,方法数为s(p-1,k-1);

也可以前p-1种物品构成k个非空循环排列,而第p个物品插入第i个物品的左边,这有(p-1)*s(p-1,k)种方法。

 

 

第二类Stirling数 S(p,k)

   

S(p,k)的一个组合学解释是:将p个物体划分成k个非空的不可辨别的(可以理解为盒子没有编号)集合的方法数。

k!S(p,k)是把p个人分进k间有差别(如:被标有房号)的房间(无空房)的方法数。

   

S(p,k)的递推公式是:S(p,k)=k*S(p-1,k)+S(p-1,k-1) ,1<= k<=p-1

边界条件:S(p,p)=1 ,p>=0    S(p,0)=0 ,p>=1

  

递推关系的说明:

考虑第p个物品,p可以单独构成一个非空集合,此时前p-1个物品构成k-1个非空的不可辨别的集合,方法数为S(p-1,k-1);

可以前p-1种物品构成k个非空的不可辨别的集合,第p个物品放入任意一个中,这样有k*S(p-1,k)种方法。

  

第一类斯特林数和第二类斯特林数有相同的初始条件,但递推关系不同。

技术分享
 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <vector> 7 #include <ctime> 8 #include <queue> 9 #include <list>10 #include <set>11 #include <map>12 using namespace std;13 #define INF 0x3f3f3f3f14 typedef long long LL;15 16 const int mod = 10056;17 LL res[1010][1010], ans[1010][1010], x[1010], y[1010];18 void init()19 {20     x[1] = 1;21     for(int i = 2; i <= 1000; i++)22         x[i] = (x[i-1] * i) % mod;23     memset(res, 0, sizeof(res));24     res[1][1] = 1, y[1] = 1;25     for(int i = 2; i <= 1000; i++)26     {27         for(int j = 1; j <= i; j++)28         {29             res[i][j] = (j*res[i-1][j]%mod + res[i-1][j-1])%mod;30             ans[i][j] = (res[i][j] * x[j]) % mod;31             y[i] = (y[i]+ans[i][j]) % mod;32         }33     }34 }35 int main()36 {37     int t, n;38     init();39     scanf("%d", &t);40     for(int ca = 1; ca <= t; ca++)41     {42         scanf("%d", &n);43         printf("Case %d: %lld\n", ca, (y[n]+mod)%mod);44     }45     return 0;46 }
View Code

 

lightoj1326_第二类Stirling