Codeforce 559 C Gerald and Giant Chess

Source: Internet
Author: User

Test instructions is probably from the upper left corner to the lower right corner can only go down or to the right, black lattice can not walk, ask how many kinds of way.


It would be easy to walk to the R line when it's not in the black lattice, and C (r+c-2,r-1).


Considering the small number of black squares, dp[i]: Walk to the first black lattice

#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;typedef long long ll;const ll Mod=1000000007ll;ll Qpow (ll A,ll b) {ll ans=1;whi Le (b) {if (b&1) ans= (ans*a)%mod;a= (a*a)%mod;b>>=1;} return ans;} ll fac[200010];ll C (int a,int b) {return Fac[a]*qpow (fac[a-b]*fac[b]%mod,mod-2)%mod;} struct Point{int r,c;} Point[2010];bool CMP (point A,point B) {return A.R!=B.R?A.R&LT;B.R:A.C&LT;B.C;} ll Dp[2010];int main () {int h,w,n;cin>>h>>w>>n;fac[0]=1;for (int i=1;i<=h+w;i++) fac[i]=fac[i-1] *i%mod;for (int i=0;i<n;i++) cin>>point[i].r>>point[i].c;sort (point,point+n,cmp);p oint[n].r=h; point[n].c=w;for (int i=0;i<=n;i++) {dp[i]=c (Point[i].r+point[i].c-2,point[i]. r-1); for (int j=0;j<i;j++) if (point[j].c<=point[i].c) {int r=point[i].r-point[j].r,c=point[i].c-point[j].c; Dp[i]= (Dp[i]-dp[j]*c (r+c,r)%mod+mod)%mod;}} Cout<<dp[n];}




Time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Gia NT Chess is quite common in geraldion. We won't delve into the rules of the game, we'll just say that the game takes place on An h ? x? W  field, and it is a painted in the colors, but not as in chess. Almost all cells of the field is white and only some of them is black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald have almost won, and the only thing he needs to win are to bring the pawn from the upper left corner of the board, WH Ere it was now standing, to the lower right corner. Gerald is so confident of victory the he became interested, in how many ways can he win?

The pawn, which Gerald have got left can go in both ways:one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There is no other pawns or pieces left on the field, so, according to the rules of giant chess Gerald moves he pawn Until the game is over, and Pollard are just watching this process.

Input

The first line of the input contains three integers: h,? W,? n -the sides of the board and the number of black cells (1?≤? H,? w. ≤?105,? 1?≤? n? ≤?2000).

NextNLines contain the description of black cells. TheI-th of these lines contains numbers Ri,? C I (1?≤? R i? ≤? h,? 1?≤? C i? ≤? W )-the number of the row and column of theI-th cell.

It is guaranteed, the upper left and lower, right cell was white, and all cells in the description were distinct.

Output

Print a single line-the remainder of the number of ways to move Gerald ' s pawn from the upper left to the lower right Cor NER modulo9? +?7.

Sample Test (s) input
3 4 22) 22 3
Output
2
Input
100 100 315 1616 1599 88
Output
545732279

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforce 559 C Gerald and Giant Chess

Related Article

Contact Us

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.