[] Generated string SCOI

A widely circulated $ trick $, but I did not learn now.

Face questions

https://www.luogu.org/problem/P1641

answer

In any of the first k characters, the number 1 must be greater than equal to the number zero.

We put a $ 1 $ considered to take a step to the right, put a $ 0 $ considered to take a step up, now converted into the answer from $ (0,0) $ start, went $ (n, m) $, and can not across the $ y = x $, how many number of programs.

We consider a similar question: is the original condition, but can not exceed $ y = x + 1 $, take the total program minus the illegal scheme directly, for the number of illegal schemes: If you cross the $ y = x + 1 $ we put it in the bottom right part of this straight line folding, so that it becomes from $ (- 1,1) $ walked several programs $ (n, m) $, and so is not difficult to find one correspondence of. Therefore, the answer to this question is $ C_ {n + m} ^ {n} -C_ {n + m} ^ {n + 1} $.

For this question, first, the first step is not up, and certainly to the right, thus converted into this model.

#include<cstdio>
#include<cstring>
#include<iostream>
#define ri register int
#define mod 20100403
using namespace std;
int n,m;

int pow(int a,int b) {
  int ret=1;
  for (;b;b>>=1,a=a*1LL*a%mod) if (b&1) ret=ret*1LL*a%mod;
  return ret;
}

int C(int n,int m) {
  int s=1,d=1;
  for (ri i=n;i>=n-m+1;i--) s=s*1LL*i%mod;
  for (ri i=1;i<=m;i++) d=d*1LL*i%mod;
  return s*1LL*pow(d,mod-2)%mod;
}

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

 

Guess you like

Origin www.cnblogs.com/shxnb666/p/11479654.html