1. 程式人生 > >10.12 考試 第一題 字符串題解

10.12 考試 第一題 字符串題解

ace aps get init 一行 b- 輸出 pan pac

字符串

時間限制: 1 Sec 內存限制: 64 MB

題目描述

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

  當時這道題真心腦子犯笨,明明是做過的原題,但就是沒反應過來,還現場打表找規律,感覺自己像個傻子一樣,先打了一個多小時找規律,沒找出來,又在打完第三題和第二題暴力後回來接著打了40分鐘的表,終於找到規律才A了,然而別人都是花了不到十分鐘A掉的,說不定如果我當時早早A掉這次可能就AK了。

  直接說結論吧,C(n+m,n)-C(m-1)。打完就沒了……

技術分享
 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <queue>
 6
#include <algorithm> 7 #include <cmath> 8 #include <map> 9 #define N 1000010 10 using namespace std; 11 int n,m,p=20100403; 12 long long jc[N*2]; 13 long long exgcd(long long a,long long b,long long c) 14 { 15 if(!a)return -1; 16 if(c%a==0)return c/a; 17 long long t=exgcd(b%a,a,(((-c%a)+a)%a));
18 if(t==-1)return t; 19 return (b*t+c)/a; 20 } 21 void init() 22 { 23 jc[0]=1; 24 for(int i=1;i<=N*2+5;i++) 25 { 26 jc[i]=(jc[i-1]*i)%p; 27 } 28 } 29 long long get_c(int a,int b) 30 { 31 return ((jc[a]*exgcd(jc[b],p,1)%p)*exgcd(jc[a-b],p,1)%p)%p; 32 } 33 long long ksm(long long x,long long z) 34 { 35 long long ans=1; 36 while(z) 37 { 38 if(z&1) 39 { 40 ans*=x; 41 ans%=p; 42 } 43 x*=x;x%=p; 44 z>>=1; 45 } 46 return ans; 47 } 48 long long f[2005][2005]; 49 int main() 50 { 51 init(); 52 scanf("%d%d",&n,&m); 53 if(n==m) 54 { 55 long long ans=get_c(n*2,n); 56 ans*=exgcd(n+1,p,1); 57 ans%=p; 58 printf("%lld\n",ans); 59 } 60 else if(n<=1000) 61 { 62 memset(f,0,sizeof(f)); 63 f[1][1]=1; 64 for(int i=2;i<=n+m;i++) 65 { 66 for(int j=(i+1)/2;j<=i&&j<=n;j++) 67 { 68 f[i][j]+=f[i-1][j-1]; 69 f[i][j]%=p; 70 if(i-j<=j)f[i][j]+=f[i-1][j]; 71 f[i][j]%=p; 72 } 73 } 74 printf("%lld\n",f[n+m][n]); 75 } 76 else 77 { 78 long long ans=get_c(m+n,m); 79 ans*=(n-m+1); 80 ans%=p; 81 ans*=exgcd(m+2+n-m-1,p,1); 82 ans%=p; 83 printf("%lld\n",ans); 84 } 85 return 0; 86 }
View Code

10.12 考試 第一題 字符串題解