zoukankan      html  css  js  c++  java
  • BZOJ1856: [Scoi2010]字符串

    1856: [Scoi2010]字符串

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 1684  Solved: 949
    [Submit][Status][Discuss]

    Description

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

    Input

    输入数据是一行,包括2个数字n和m

    Output

    输出数据是一行,包括1个数字,表示满足要求的字符串数目,这个数可能会很大,只需输出这个数除以20100403的余数

    Sample Input

    2 2

    Sample Output

    2

    HINT

    【数据范围】
    对于30%的数据,保证1<=m<=n<=1000
    对于100%的数据,保证1<=m<=n<=1000000

    思路{

      我们不妨把它在平面坐标系上抽象出来,

      从(0,1)开始用1表示走(1,1),0表示(1,-1);

      那么所求就是从(0,1)到(n+m,n-m)不经过x轴的方案数=(0,1)到(n+m,n-m)不经过x轴的方案数-(0,1)到(n+m,n-m)经过x轴的方案数.

      (0,1)到(n+m,n-m+1)的方案数=c(n+m,m);

      而(0,1)到(n+m,n-m)经过x轴的方案数又相当于

      把从起点到x轴翻转=从(0,-1)经过x轴的方案数到(n+m,n-m-2),

      算出向上走n-1步,那么(0,1)到(n+m,n-m)经过x轴的方案数=c(n+m,n-1);

      故ans=c(n+m,m)-c(n+m,n-1);

    }

    #include<bits/stdc++.h>
    #define RG register
    #define il inline 
    #define mod 20100403
    #define N 2000010
    #define LL long long
    using namespace std;
    LL fac[N];
    void pre(){
      fac[1]=fac[0]=1;
      for(int i=2;i<N;++i)fac[i]=fac[i-1]*i,fac[i]%=mod;
    }
    LL qp(LL a,LL b){
      if(b==1)return a%mod;
      if(!b)return 1;
      LL tmp=qp(a,(b>>1));
      tmp=tmp*tmp%mod;
      if(b&1)tmp*=a,tmp%=mod;
      return tmp;
    }
    LL ni(LL x){return qp(x,mod-2);}
    LL calc(LL m,LL n){
      return (fac[n]*ni((fac[m]*fac[n-m])%mod))%mod;
    }
    int main(){
      pre();
      int n,m;scanf("%d%d",&n,&m);
      cout<<(calc(n,n+m)-calc(n+1,n+m)+mod)%mod;
      return 0;
    }
    
  • 相关阅读:
    msyqld 的 The user specified as a definer ('root'@'%') does not exist 问题
    Python加密模块-pycryptodome
    【leetcode 简单】 第一百一十题 分发饼干
    Python数据类型-字典
    Python数据类型-集合(set)
    Python数据类型-列表(list)增删改查
    Python数据类型-元组
    Python 函数系列- Str
    Linux运维之shell脚本
    python之面向对象篇6
  • 原文地址:https://www.cnblogs.com/zzmmm/p/7476512.html
Copyright © 2011-2022 走看看