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

    Description:

    lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数。现在lxhgww想要知道满足要求的字符串共有多少个,聪明的程序员们,你们能帮助他吗?

    Hint:

    (n,m le 10^6)

    Solution:

    很妙的一道题

    网格图上的组合数学

    直接做发现不可做,考虑转化,借的图:

    我们把初始状态设为原点,最终状态设为((n+m,n-m))

    选1代表向右上走,走选0代表向左上走

    答案就是走到终点,且不经过(y=-1)这条直线的方案数(想一想,为什么?)

    这不好求,转化求总方案减去不合法方案,求经过(y=-1)这条直线的方案数

    然后不难发现,路径经过(y=-1)的方案就等于从((0,-2))出发的方案

    列个方程就知道要向下走m-1步,即方案为(C(n+m,m-1))

    所以答案就是(C(n+m,m)-C(n+m,m-1))

    #include <map>
    #include <set>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #define ls p<<1 
    #define rs p<<1|1
    using namespace std;
    typedef long long ll;
    const int mxn=2e6+5,mod=20100403;
    int n,m,fac[mxn],ifac[mxn];
    inline int read() {
        char c=getchar(); int x=0,f=1;
        while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
        while(c<='9'&&c>='0') {x=(x<<3)+(x<<1)+(c&15);c=getchar();}
        return x*f;
    }
    inline int chkmax(int &x,int y) {if(x<y) x=y;}
    inline int chkmin(int &x,int y) {if(x>y) x=y;}
    
    int qpow(int a,int b) 
    {
        int res=1,bs=a;
        while(b) {
            if(b&1) res=1ll*res*bs%mod;
            bs=1ll*bs*bs%mod;
            b>>=1;
        }
        return res;
    }
    
    inline ll C(int n,int m) {
        return 1ll*fac[n]*ifac[m]%mod*ifac[n-m]%mod;
    }
    
    int main()
    {
        cin>>n>>m;
        fac[0]=ifac[0]=1; 
        for(int i=1;i<=n+m;++i) fac[i]=1ll*fac[i-1]*i%mod;
        ifac[n+m]=qpow(fac[n+m],mod-2);
        for(int i=n+m-1;i>=1;--i) ifac[i]=1ll*ifac[i+1]*(i+1)%mod;
        printf("%lld",(C(n+m,m)-C(n+m,m-1)+mod)%mod);
        return 0;
    }
    
    
  • 相关阅读:
    Comprehend-Elasticsearch-Demo5
    Mxnet使用TensorRT加速模型--Mxnet官方例子
    Mxnet模型转换ONNX,再用tensorrt执行前向运算
    MxNet模型转换Onnx
    基于Flask-APScheduler实现添加动态定时任务
    Golang习题
    算法题
    Celery使用指南
    flask拓展(数据库操作)
    flask进阶(上下文源管理源码浅析)
  • 原文地址:https://www.cnblogs.com/list1/p/10540834.html
Copyright © 2011-2022 走看看