卡特蘭數 兩個經典做法

2021-10-24 19:13:27 字數 1690 閱讀 3980

卡特蘭數現在我能接觸到的用處就是用於解決乙個序列中,任意字首和中1的數量不少於0的數量(在這個序列中,1與0的個數相等)

證明如下圖:

卡特蘭數 =(公式一) c(2* n , n) - c(2n , n- 1) = (公式二)c(2n , n) / (n + 1)

組合數的板子很多,我們這裡簡單說3個吧

模板一 :組合數求余大質數

求c(a,b)

時間複雜度: o(a * loga)

#include#include#include#includeusing namespace std;

typedef long long ll;

const int n = 2015;

int n,mod = 1e9 + 7;

int fact[n],infact[n];

int qmi(int a, int k)

return res;

}int main()

cin >> t;

while(t --)

return 0;

}

模板二:組合數對任意素數求餘p (p 不定)

時間複雜度:o(n * logn)

#include

#include

#include

#include

#include

using

namespace std;

const

int n =

2e6+15;

typedef

long

long ll;

ll primes[n]

,cnt;

bool book[n]

;ll n,mod;

void

get_primes()

}}ll get

(ll x,ll p)

return res;

}ll qmi

(ll a, ll b)

return ans;

}ll c

(ll a, ll b)

return res;

}int

main()

模板三:超大(1e18)組合數求餘p(p為質數)

lucas定理:c(a,b) = c(a % p, b % p) * c(a / p, b / p);

時間複雜度:接近logpn * logp

#include

#include

#include

#include

using

namespace std;

typedef

long

long ll;

int p;

intqmi

(int a,

int b)

return cnt;

}intc(

int a,

int b)

return res;

}int

lucas

(ll a,ll b)

intmain()

return0;

}

OJ經典題目 卡特蘭數

卡特蘭數又稱卡塔蘭數,卡特蘭數是組合數學中乙個常出現在各種計數問題中的數列。以比利時的數學家歐仁 查理 卡塔蘭 1814 1894 的名字來命名。原理如下 設h n 為catalan數的第n 1項,令h 0 1,h 1 1,catalan數滿足遞推式 2 h n h 0 h n 1 h 1 h n ...

卡特蘭數 什麼鬼,又是乙個板子

一 關於卡特蘭數 卡特蘭數是一種經典的組合數,經常出現在各種計算中,其前幾項為 1,2,5,14,42,132,429,1430,4862,16796,58786,208012,742900,2674440,9694845,35357670,129644790,477638700,176726319...

兩個lock的經典使用示例

示例一 public class numberprintdemo catch interruptedexception e 當state 1時,輪到執行緒1列印5次數字 for int j 0 j 5 j system.out.println 執行緒1列印完成後,將state賦值為2,表示接下來將輪...