[51nod1486] large Lattice

Source: Internet
Author: User

There is a board with W columns in row H, and there are some grids in it that cannot be taken. Now we need to go from the upper left corner () to the lower right corner (H, W.

Input
Test data in a single group.
The first row has three integers: H, W, n (1 ≤ h, W ≤ 10 ^ 5, 1 ≤ n ≤ 2000), indicating the rows and columns of the Board, there is also a number of cells that cannot be taken.
The next n rows describe the lattice. line I has two integers Ri, CI (1 ≤ rI ≤ h, 1 ≤ CI ≤ W), indicating the row and column of the grid.
Input to ensure that there is no grid that cannot be taken between the start point and the end point.
Output
The output result returns the remainder of the 1000000007 answer pair.

 

 

Combined mathematics. If ans [I] is set, it indicates the number of solutions that go from (1, 1) to the I-th lattice and do not pass through other obstacles. (H, W) is also regarded as an obstacle)

Ans [I] = C (X [I]-1 + Y [I]-1, x [I]-1) -sum {ans [J] * C (X [I]-X [J] + Y [I]-y [J], X [I]-X [J])}. Point J is in the upper-left corner of vertex I (including the left or top ).

 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #define ll long long 7 #define ull unsigned long long 8 #define ui usigned int 9 #define d double10 #define ld long double11 const int maxn=2023,modd=1000000007;12 struct zs{int x,y;}a[maxn];13 int jc[200233],ny[200233],ans[maxn];14 int i,j,k,n,m;15 16 int ra,fh;char rx;17 inline int read(){18     rx=getchar(),ra=0,fh=1;19     while(rx<‘0‘&&rx!=‘-‘)rx=getchar();20     if(rx==‘-‘)fh=-1,rx=getchar();21     while(rx>=‘0‘)ra=ra*10+rx-48,rx=getchar();return ra*fh;22 }23 24 inline int getc(int n,int m){25     return 1ll*jc[n]*ny[n-m]%modd*ny[m]%modd;26 }27 inline int get(int x,int y){28     return getc(x+y,y);29 }30 inline int poi(int a,int b){31     int c=1;32     while(b){33         if(b&1)c=1ll*c*a%modd;34         a=1ll*a*a%modd,b>>=1;35     }return c;36 }37 bool operator <(zs a,zs b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}38 inline void UPD(int &x){if(x<0)x+=modd;}39 int main(){40     n=read(),m=read();int num=read();register int i,j;41     for(i=jc[0]=ny[0]=1;i<=n+m;i++)jc[i]=1ll*jc[i-1]*i%modd,ny[i]=poi(jc[i],modd-2);42 //    for(i=1;i<=n||i<=m;i++)for(j=1;j<=i;j++)printf("%d %d  c:%d\n",i,j,getc(i,j));43     for(i=1;i<=num;i++)a[i].x=read()-1,a[i].y=read()-1;44     a[++num]=(zs){n-1,m-1};45     std::sort(a+1,a+1+num);46     for(i=1;i<=num;i++){47         ans[i]=get(a[i].x,a[i].y);//printf("(%d,%d) %d\n",a[i].x,a[i].y,ans[i]);48         for(j=1;j<i;j++)if(a[j].y<=a[i].y)49             UPD(ans[i]-=1ll*ans[j]*get(a[i].x-a[j].x,a[i].y-a[j].y)%modd);50     }51     printf("%d\n",ans[num]);52 }
View code

 

[51nod1486] large Lattice

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.