bzoj1856 [Scoi2010]字符串

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1856

【题解】

考虑将操作看成走(1, -1)和(1, 1),那么就是从(0, 0)走到(n+m, n-m)。

那么有x+y=n+m,x-y=n-m,那么x=n, y=m。

那么方案数为C(n+m, m)。

减去不合法的方案,即经过y=-1。

将线关于y=-1对称,那么就是从(0, -2)走到(n+m, n-m)

那么有x+y=n+m, x-y=n-m+2

那么有x=n+1, y=m-1

那么方案就是C(n+m, m-1)。

那么方案就是两个相减。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10;
const int mod = 20100403;

# define RG register
# define ST static

// (0,0) ==> (n+m, n-m)
// C(n+m, m)
// (-2, 0) ==> (n+m, n-m)
// C(n+m, m-1)

int n, m, fu, fd;

inline int pwr(int a, int b) {
    int ret = 1;
    while(b) {
        if(b&1) ret = 1ll * ret * a % mod;
        a = 1ll * a * a % mod;
        b >>= 1;
    }
    return ret;
}

inline int C(int n, int m) {
    fu = 1; fd = 1;
    for (int i=m+1; i<=n; ++i) fu = 1ll * fu * i % mod;
    for (int i=1; i<=n-m; ++i) fd = 1ll * fd * i % mod;
    fu = 1ll * fu * pwr(fd, mod-2) % mod;
    return fu;
}

int main() {
    cin >> n >> m;
    cout << ((C(n+m, m) - C(n+m, m-1)) % mod + mod) % mod << endl;
    return 0;
}
View Code

 

posted @ 2017-05-16 11:51  Galaxies  阅读(188)  评论(0编辑  收藏  举报