zoukankan      html  css  js  c++  java
  • [题解] [SCOI2010] 生成字符串

    题面

    题解

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

    总方案就是(inom{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), 所以方案数是(inom{n+m}{n+1})

    所以总方案数就是(inom{n+m}{n}-inom{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
    ", ((C(n + m, n) - C(n + m, n + 1)) % mod + mod) % mod); 
    	return 0;
    }
    
  • 相关阅读:
    PyTorch-->Tensors(张量)
    Pytorch-->PyTorch学习(1)
    信息安全四大顶会!!!
    2020春季数据库-->关系演算安全性的理解
    2020春季数据库-->三级模式和两层映像
    2020寒假->Web编程(1)
    Httprequseter 插件使用
    YII2 客户端验证
    谷歌网页全屏截图
    后台新订单提醒
  • 原文地址:https://www.cnblogs.com/ztlztl/p/11186148.html
Copyright © 2011-2022 走看看