[P1641] Luo Valley [SCOI2010] generated string (Catalan number)

Title link
can be seen from a coordinate system \ ((0,0) \) a \ (n + m \) Step go \ ((n + m, nm ) \) the number of programs, only the right \ ((1) \) or lower right \ ((0) \) down, and can not go \ (y = -1 \) on this line.
Finally, do not consider the limitations of the word is \ (n + m \) times selected \ (m \) times right laid down, namely \ (C (the n-+ m, m) \) .
Then according to the principles of symmetry, from \ ((0,0) \) come \ (y = -1 \) is equivalent to from \ ((0, -2) \) come \ (y = -1 \) , the number of programs which is the same, so after \ (y = -1 \) is the number of programs actually \ ((0, -2) \) come \ ((n + m, nm ) \) scheme number, i.e. \ (C (n + m,
m-1) \) so that only the title labels "inverse element" is only used to find the number of combinations only.

#include <cstdio>
#define ll long long
int n, m;
const int MOD = 20100403;
void exgcd(ll &x, ll &y, ll a, ll b){
    if(!b){ x = 1; y = 0; return; }
    exgcd(x, y, b, a % b);
    ll z = x; x = y; y = z - a / b * y;
}
int fact[2000010];
ll x, y, X, Y;
int C(int n, int m){
    exgcd(x, y, fact[m], MOD); exgcd(X, Y, fact[n - m], MOD);
    return ((ll)fact[n] * x % MOD * X % MOD + MOD) % MOD;
}
int main(){
    fact[0] = 1;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= 2000000; ++i)
        fact[i] = (ll)fact[i - 1] * i % MOD;
    printf("%d\n", ((C(n + m, m) - C(n + m, m - 1)) % MOD + MOD) % MOD);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Qihoo360/p/11228270.html