# [Train of thought + number of combinations] HDU 3398 String

Source: Internet
Author: User

Test instructions

give you n 1 and M 0, let you spell the length of the n+m and all the prefixes of 1 are not less than 0 of the number of strings, ask how many ways.

Ideas:

The process of constructing the string is converted to two-dimensional coordinates, 1 with y, 0 by X, from the coordinates (0,0), 0 for the right (x increase), 1 for the upward Walk (y increase), because 0 has m, 1 has n, so the last reached coordinates are

(M,n), the simple consideration from 0,0 to M,n altogether has C (n+m,m) method, and because any prefix in the number of 1 can not be less than 0, so the y>=x, that is, the path of the legal go through the coordinates of either on the Y=x, or on its top, then

Illegal routes through the coordinates are satisfied y<x, that is, the route can not intersect with the y=x-1, because as long as the intersection, will not meet the number of 1 can not be less than 0.

So the illegal path must intersect with the Y=x-1, find (0,0) The symmetry point about Y=x-1 (1,-1), go from (1,-1) to (M,n) must intersect with the line y=x-1, because M,n is on the top of the line, 1,-1 under the line

Side. In this case, the intersection is P, then the route is divided into two parts of P, the upper part of the name is called illegal route, the following part of the name is called the legal route. So how many ways do you get from 1,-1 to M,n?

Is the number of illegal methods that go from 0,0 to M,n. Think carefully about why. Why to find the symmetry point, in the symmetrical straight side of the route can always find the opposite side of the line and symmetry, just now we went from 1,-1 to M,n

The legal route is symmetrical along the y=x-1, and the illegal route is not symmetrical, because the illegal route has intersected with the y=x-1 (in fact, the last point of the legal route also intersects with the y=x-1), so there are 0, 0 to M,n

A scheme, in general, for each route from 1,-1 to M,n, there is a route from 0, 0 to m,n about y=x-1 symmetry. Walk from 1,-1 to M,n method number is C (m+n,m-1).

So the answer to this question is C (n+m,m)-C (m+n,m-1).

Code:

```#include "cstdlib" #include "Cstdio" #include "CString" #include "Cmath" #include "queue" #include "algorithm" #include "
iostream "#include" map "#include" stack "#define N 2000003 #define EPS 1e-8 #define MOD 20100501 using namespace std;
#define LL __int64 #define MAXN 20000007 bool MARK[MAXN];
int SS[MAXN/3],SSCNT,SUM[MAXN/3];
void SSB () {sscnt=0;
memset (Mark,false,sizeof (Mark));
Mark=mark=true; for (int i=2, i<=maxn; i++) {if (!mark[i]) {for (int j=i+i; j<=maxn; j+=i) mark[j]=tr
Ue
Ss[sscnt++]=i;
}} return;
} ll Power (ll A,ll b) {ll ans=1;
while (b) {if (b&1) ans= (ans*a)%mod;
A= (a*a)%mod;
b>>=1;
} return ans;
} ll Yzs (ll X,ll y) {ll ans=x/y;
if (x<y) return ans;
Return Ans+=yzs (x/y,y);
} LL FC (ll N,ll m) {if (n<m) return 0;
ll Ans=1;
for (int i=0, i<sscnt; i++) {if (ss[i]>n) break; Ans*=power (ss[I],yzs (N,ss[i])-yzs (M,ss[i])-yzs (n-m,ss[i]));
Ans%=mod;
} return ans;
} int main () {int t;
cin>>t;
SSB ();
while (t--) {ll n,m;
scanf ("%i64d%i64d", &n,&m);
ll A=1,b=1;
A=FC (N+m,n);
B=FC (n+m,n+1);
ll Ans= ((A-B)%mod+mod)%mod;
printf ("%i64d\n", ans);
} return 0;
}```

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.