HDU 1133 卡特蘭數

來源:互聯網
上載者:User

卡特蘭數的應用~~~ ^_^
1133 公式推導如下 ://MiYu原創, 轉帖請註明 : 轉載自
______________白白の屋

( C(m+n, n)
- C(m+n, m+1)
) * m!
* n! 化簡即 (m+n)!
* (m-n+1)
/ (m+1)

推導過程如下 :

m個人拿50,n個人拿100

1: 所以如果 n
> m,那麼排序方法數為
0 這一點很容易想清楚

2: 現在我們假設 拿50的人用 ‘0’表示, 拿100的人用
1 表示。

如果有這麼一個序列 0101101001001111.

當第K個位置出現1的個數多餘0的個數時就是一個不合法序列了

假設m=4 n=3的一個序列是:0110100
顯然,它不合法, 現在我們把它稍微變化一下:

把第二個1(這個1前面的都是合法的)後面的所有位0變成1,1變成0

就得到 0111011 這個序列1的數量多於0的數量, 顯然不合法, 但現在的關鍵不是看這個序列是不是合法的

關鍵是:它和我們的不合法序列 0110100 成一一對應的關係

也就是說任意一個不合法序列(m個0,n個1), 都可以由另外一個序列(n-1個0和m+1個1)得到

另外我們知道,一個序列要麼是合法的,要麼是不合法的

所以,合法序列數量 = 序列總數量
- 不合法序列的總量

序列總數可以這樣計算m+n 個位置中, 選擇 n 個位置出來填上
1, 所以是 C(m+n, n)

不合法序列的數量就是: m+n 個位置中, 選擇 m+1 個位置出來填上
1 所以是 C(m+n, m+1)

然後每個人都是不一樣的,所以需要全排列 m!
* n!

所以最後的公式為 : ( C(m+n, n)
- C(m+n, m+1)
) * m!
* n! 化簡即 (m+n)!
* (m-n+1)
/ (m+1)

推廣:
如果原來有p張50元的話,那麼不合法的序列的數量應該是:任意一個不合法序列(m個0,n個1),

都可以由另外一個序列(n-1個0和m+1+p個1)得到,所以是m+n
個位置中, 選擇 m+1+p 個位置

出來填上 1 所以是 C(m+n, m+1+p)
接下來的化簡就不推了.
Code:#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<stdio.h>
#include<string.h>
using namespace std;
const int Base=10000;
//萬進位 int 最大,如果只用 +,-,可加大Base=1000000000
int ONE[]={1,1}; //大整數 1
int ZERO[]={1,0}; //大整數 0
//大整數比較大小
int comp(int *a, int *b) {
    int i;
    if (a[0] > b[0])return 1;
    if (a[0] < b[0])return -1;
    for (i = a[0]; i >= 1; i--) {
        if (a[i] > b[i])return 1;
        if (a[i] < b[i])return -1;
    }
    return 0;
}
int comp(int *a, int *b,int t) {
    int i,j=1;
    for (i = b[0]; i >= 1; i--,j++) {
        if (a[t] > b[i])return j;
        if (a[t] < b[i])return -1;
        t--;
    }
    return 0;
}

void PN(int *a){
 int i;//輸出 "%04d" 10000進位
 printf("%d", a[a[0]]);
 for(i=a[0]-1;i>=1;i--)printf("%04d", a[i]);printf("\n");
}

void copy(int *a, int *b)
{ int i;  //a=b
  for (i = 0;i<= b[0]; ++i)a[i] = b[i];
}

//大整數a乘常數 b b<Base, d=a*b
void mult(int *a,int b,int *d)
{ int w, i,p;
  if(b==1){copy(d,a);return;}
  if((b==0)||(a[0]==1&&a[1]==0)){d[0]=1;d[1]=0;return;}
  w = a[0];
  p = 0; //
  for (i = 1; i <= w; i++)
  { d[i] = a[i] * b + p;
    if (d[i] >= Base)
    { p = d[i] / Base; d[i] %= Base;}else p=0;
  }
  if (p) {w++;d[w] = p;}
  d[0] = w;
}
// c=a/b   k=a%b
int Div(int *a,int b,int *c)
{int i,j,s,t;
 int k=0,q;
// long long k=0,q;  Base>=100000
 if(comp(a,ZERO)==0){copy(c,ZERO);return 0;}
 for(i=a[0];i>=1;i--)  
 {q=k*Base+a[i];
  c[i]=q/b;k=q%b;  
 }
 if(c[a[0]]==0)
 { c[0]=a[0]-1;
 }else c[0]=a[0];
 return k;
}

int a[10000],b[10000],c[10000];

int main(){
    int ca=1;
    int n,m;
    while(scanf("%d%d",&m,&n)!=EOF){
        if(m+n==0) break;
        printf("Test #%d:\n",ca++); 
        a[0]=1,a[1]=1;
        if(n>m){
            printf("0\n");
            continue;
        }
        for(int i=2;i<=m+n;i++){
            mult(a,i,b);
            copy(a,b);
        }
        mult(a,m-n+1,b);
        copy(a,b);        
        Div(a,m+1,c);        
        PN(c);
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.