UVA - 11077 Find the Permutations 置換羣+斯特林數

我們可以將這個排列寫成循環的形式,可知,一個大小爲x循環,需要對換x-1次才能對換完成

所以題目的意思就是有多少種排列有n-k個循環,等價於n個數分成n-k個園排列的方案數,這正是第一類斯特林數。。不過要注意,,這題要用ULL。。

#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<queue>
#include<string.h>
#include<string>
#include<cstring>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<x<<endl;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int,int>,int> PIII;
typedef pair<int,int> PII;
const double eps=1e-5;
const int P=1e9+7;
const int MX=1e6+5;
int n,k;
ULL S[33][33];
int main()
{
    S[0][0]=1;
    for(int i=0; i<=21; i++)
        for(int j=0; j<=21; j++)
        {
            if(i==0&&j==0)continue;
            S[i][j]=0;
            if(i-1>=0&&j-1>=0)S[i][j]+=S[i-1][j-1];
            if(i-1>=0)S[i][j]+=(i-1)*S[i-1][j];
        }
    while(cin>>n>>k&&n)
    {
        cout<<S[n][n-k]<<endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章