[题解] [SCOI2010] 生成字符串

题面

题解

考虑到直接求合法方案不好求, 我们转化为用总方案减去不合法方案

总方案就是\(\binom{n+m}{m}\), 即在\(n+m\)个位置中放\(n\)个数

我们将初始的空序列看做\((0, 0)\), 选\(1\)代表\((+1,+1)\), 选\(0\)代表\((+1,-1)\)

那么不合法的方案就是经过\(y = -1\)的方案, 考虑如何转化

恩, 看到这张图片没, 在这条折线第一次经过\(y = -1\)时将其翻转

可以知道终点不变, 为\((n + m, n - m)\)

起点因为翻转变为\((0, -2)\)

则向右上走总操作次数变为\(n + 1\), 所以方案数是\(\binom{n+m}{n+1}\)

所以总方案数就是\(\binom{n+m}{n}-\binom{n+m}{n+1}\)

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#define itn int
#define reaD read
#define mod 20100403
#define N 2000005
using namespace std;

int n, m, jc[N]; 

inline int read()
{
	int x = 0, w = 1; char c = getchar();
	while(c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); }
	while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
	return x * w;
}

int fpow(int x, int y)
{
	int res = 1;
	while(y)
	{
		if(y & 1) res = 1ll * res * x % mod;
		x = 1ll * x * x % mod;
		y >>= 1; 
	}
	return res; 
}

int C(int n, int m)
{
	int ans = jc[n];
	ans = 1ll * ans * fpow(jc[m], mod - 2) % mod;
	ans = 1ll * ans * fpow(jc[n - m], mod - 2) % mod;
	return ans; 
}

int main()
{
	n = read(); m = read();
	for(int i = (jc[0] = 1); i <= n + m; i++) jc[i] = 1ll * jc[i - 1] * i % mod; 
	printf("%d\n", ((C(n + m, n) - C(n + m, n + 1)) % mod + mod) % mod); 
	return 0;
}
posted @ 2019-07-14 22:21  ztlztl  阅读(152)  评论(0编辑  收藏  举报