相关公式
H [ 0 ] = H [ 1 ] = 1 ( 1 ) H[0]=H[1]=1\quad(1) H[0]=H[1]=1(1)
H [ i ] = H [ i − 1 ] ∗ ( 4 ∗ i − 2 ) ( i + 1 ) ( i > = 2 ) ( 2 ) H[i]=\frac{H[i-1]*(4*i-2)}{(i+1)}\quad(i>=2)\quad(2) H[i]=(i+1)H[i−1]∗(4∗i−2)(i>=2)(2)
H [ i ] = C 2 i i i + 1 ( i > = 2 ) ( 3 ) H[i]=\frac{C_{2i}^{i}}{i+1}\quad(i>=2)\quad(3) H[i]=i+1C2ii(i>=2)(3)
H [ i ] = C 2 i i − C 2 i i − 1 ( i > = 1 ) ( 4 ) H[i]=C_{2i}^{i}-C_{2i}^{i-1}\quad(i>=1)\quad(4) H[i]=C2ii−C2ii−1(i>=1)(4)
H [ i ] = ∑ j = 1 i H [ j − 1 ] H [ i − j ] ( i > = 2 ) ( 5 ) H[i]=\sum_{j=1}^{i}H[j-1]H[i-j]\quad(i>=2)\quad(5) H[i]=j=1∑iH[j−1]H[i−j](i>=2)(5)
问题总结
1.n个不同数字依次入栈,求出栈的方案数
洛谷P1044(公式2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T>
void out(T x) { cout << x << endl; }
ll fast_pow(ll a, ll b, ll p) {ll c = 1; while(b) { if(b & 1) c = c * a % p; a = a * a % p; b >>= 1;} return c;}
ll exgcd(ll a, ll b, ll &x, ll &y) { if(!b) {x = 1; y = 0; return a; } ll gcd = exgcd(b, a % b, y, x); y-= a / b * x; return gcd; }
ll f[30];
int main()
{
ios::sync_with_stdio(false);
f[1] = f[0] = 1;
for(int i = 2; i <= 18; i ++)
f[i] = f[i - 1] * (4 * i - 2) / (i + 1);
int n;
cin >> n;
cout << f[n] << endl;
}
2.长度为2n的序列,有两种元素,要求在
[
1
,
i
]
[1,i]
[1,i]区间内,某种元素的数量一直大于等于另一元素的数量
洛谷P1722(公式5)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T>
void out(T x) { cout << x << endl; }
ll fast_pow(ll a, ll b, ll p) {ll c = 1; while(b) { if(b & 1) c = c * a % p; a = a * a % p; b >>= 1;} return c;}
ll exgcd(ll a, ll b, ll &x, ll &y) { if(!b) {x = 1; y = 0; return a; } ll gcd = exgcd(b, a % b, y, x); y-= a / b * x; return gcd; }
const int N = 2e2 + 5;
const int mod = 100;
ll H[N];
int main()
{
ios::sync_with_stdio(false);
H[0] = H[1] = 1;
int n;
cin >> n;
for(int i = 2; i <= n; i ++)
{
for(int j = 0; j <= i - 1; j ++)
H[i] = (H[i] + H[j] * H[i - j - 1]) % mod;
}
cout << H[n] << endl;
}
3.2n个点,连成n个线段,要求线段均不相交的方案数
洛谷P1976(公式3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T>
void out(T x) { cout << x << endl; }
ll fast_pow(ll a, ll b, ll p) {ll c = 1; while(b) { if(b & 1) c = c * a % p; a = a * a % p; b >>= 1;} return c;}
ll exgcd(ll a, ll b, ll &x, ll &y) { if(!b) {x = 1; y = 0; return a; } ll gcd = exgcd(b, a % b, y, x); y-= a / b * x; return gcd; }
const int N = 6e3 + 5;
const int mod = 1e8 + 7;
ll fac[N], inv[N];
void init()
{
fac[0] = inv[0] = 1;
for(int i = 1; i < N; i ++)
fac[i] = fac[i - 1] * i % mod;
inv[N - 1] = fast_pow(fac[N - 1], mod - 2, mod);
for(int i = N - 2; i >= 1; i --)
inv[i] = inv[i + 1] * (i + 1) % mod;
}
ll C(int n, int m)
{
return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main()
{
ios::sync_with_stdio(false);
init();
int n;
cin >> n;
cout << C(2 * n, n) * fast_pow(n + 1, mod - 2, mod) % mod << endl;
}