E. Massage

JSZKC feels so bored in the classroom that he wants to send massages to his girl friend. However, he can't move to his girl friend by himself. So he has to ask his classmates for help.

The classroom is a table of size N \times MN×M. We'll consider the table rows numbered from top to bottom 11 through NN, and the columns numbered from left to right 11 through MM. Then we'll denote the cell in row xx and column yyas (x, y)(x,y). And every cell sits a student.

Initially JSZKC sits on the cell (1, 1)(1,1) . And his girl friend sits on cell (n, m)(n,m). A message can go from cell (x, y)(x,y) to one of two cells (x + 1, y)(x+1,y) and (x, y + 1)(x,y+1). JSZKC doesn't want to trouble his classmates too much. So his classmates(not including his girl friend) may not take massages more than once. It's obvious that he can only send out two massages. Please help JSZKC find the number of ways in which the two massages can go from cell (1, 1)(1,1) to cell (n, m)(n,m).

More formally, find the number of pairs of non-intersecting ways from cell (1, 1)(1,1) to cell (n, m)(n,m) modulo 10000000071000000007 . Two ways are called non-intersecting if they have exactly two common points — the starting point and the final point.

Input Format

The input file contains several test cases, each of them as described below.

  • The first line of the input contains one integers N(2 \le N,M \le 1000)(2N,M1000), giving the number of rows and columns in the classroom.

There are no more than 100100 test cases.

Output Format

One line per case, an integer indicates the answer mod 1000000007

样例输入

2 2
2 3
3 3

样例输出

1
1
3

题目来源

The 2018 ACM-ICPC China JiangSu Provincial Programming Contest

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <utility>
 7 #include <vector>
 8 #include <map>
 9 #include <queue>
10 #include <stack>
11 #include <cstdlib>
12 typedef long long ll;
13 #define lowbit(x) (x&(-x))
14 #define ls l,m,rt<<1
15 #define rs m+1,r,rt<<1|1
16 using namespace std;
17 const ll mod=1e9+7;
18 /*
19 A1(1,2) A2(2,1)
20 B1(n-1,m)  B2(n,m-1)
21 并且只能从A1到B1,从A2到B2.组合数问题。
22 由容斥知:要再减去有交点的路线。
23 分析:
24 如果两条路线有交点,那么A1一定可以到B2,A2一定会到B1
25 */
26 
27 ll n,m;
28 const int N=3000;
29 ll f[N];
30 void init(){
31     f[0]=1;
32     for(int i=1;i<=2500;i++){
33         f[i]=(f[i-1]*i)%mod;
34     }
35 }
36 ll mul(ll a,ll b){
37     a%=mod;b%=mod;
38     return a*b%mod;
39 }
40 ll mul1(ll a,ll b,ll c){
41     a%=mod;b%=mod;c%=mod;
42     return   a*b%mod*c%mod;
43 }
44 void egcd(ll a,ll b,ll&x,ll &y){
45     ll d;
46     if(!b){
47         x=1;y=0;
48         return ;
49     }
50     else{
51         egcd(b,a%b,y,x);
52         y-=(a/b)*x;
53     }
54 }
55 ll inv(ll n){
56     ll x,y;
57     egcd(n,mod,x,y);
58     return  (x%mod+mod)%mod;
59 }
60 ll solve(ll n,ll m){
61     
62     ll a=f[n],b=inv(f[m]),c=inv(f[n-m]);
63     return mul1(a,b,c);
64 }
65 int main()
66 {   init();
67     while(~scanf("%lld%lld",&n,&m)){
68         ll ans1=solve(n+m-4,n-2);
69         ll ans2=solve(n+m-4,n-1);
70         ll ans3=solve(n+m-4,n-3);
71         ll ans4=(mul(ans1,ans1)-mul(ans2,ans3)+mod)%mod;
72         printf("%lld\n",ans4);
73     }
74 }

猜你喜欢

转载自www.cnblogs.com/tingtin/p/9363423.html