首页 > 其他 > 详细

Codeforces-559C Gerald and Giant Chess

时间:2019-04-10 15:46:49      阅读:143      评论:0      收藏:0      [点我收藏+]
Gerald and Giant Chess
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we‘ll just say that the game takes place on an h × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?

The pawn, which Gerald has got left can go in two 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 are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is 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).

Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w) — the number of the row and column of the i-th cell.

It is guaranteed that the upper left and lower right cell are white and all cells in the description are 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 corner modulo 109 + 7.

Examples
Input
Copy
3 4 2
2 2
2 3
Output
Copy
2
Input
Copy
100 100 3
15 16
16 15
99 88
Output
Copy
545732279


题目大意:从点(1,1)走到(h,w),只能向右或向下,给出n个黑点点,不经过这些点走到(h,w)的路径有多少条(mod 1e9+7)
1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000

思路:对于每个点i(x1,y1),若不考虑黑点,从(1,1)到i有C(x1+y1-2)(x1-1)条路径
考虑黑点,记s[i] = C(x1+y1-2)(x1-1),若存在点j(x2,y2)在点i的左上,则s[i]应该减去C(x1-x2+y1-y2)(x1-x2)*s[j],减去所有点i左上的黑点的影响,即得到s[i]

从右下第一个黑点计算到点(h,w)

C(x+y)(x) = c[x+y]*inv[x]*inv[y]%mod
c[k]:k的阶乘
inv[k]:k的逆元

逆元由公式inv[i] = inv[i+1] * (i+1) % mod 求得
技术分享图片
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
const int N = 2e5+10;
struct Node{
    int r;
    int c;
}node[2005];
long long s[2005],c[N],inv[N];
bool cmp(Node a,Node b){
    if(a.c==b.c) return a.r<b.r;
    return a.c<b.c;
}
void Getfac(int d){
    c[0]=1;
    for(int i = 1;i<=d;i++){
        c[i]=c[i-1]*i%mod;
    }
}
long long quick_inv(int n){
    long long ans = 1,pos = n;
    int y = mod - 2;
    while(y){
        if(y%2) ans = ans*pos%mod;
        pos = pos*pos%mod;
        y = y/2;
    }
    return ans%mod;
}
void Getinv(int d){
    inv[d] = quick_inv(c[d]);
    for(int i = d-1;i>=0;i--){
        inv[i] = inv[i+1]*(i+1)%mod;
    }
}
int main()
{
    int h,w,n;
    long long k,t;
    scanf("%d%d%d",&h,&w,&n);
    for(int i = 1;i <= n;i++){
        scanf("%d%d",&node[i].r,&node[i].c);
    }
    node[0].r = 1;
    node[0].c = 1;
    node[n+1].r = h;
    node[n+1].c = w;
    s[0] = 1;
    sort(node+1,node+n+1,cmp);
    Getfac(h+w);
    Getinv(h+w);
    for(int i = 1;i <= n+1;i++){
        k = node[i].c-1;
        t = node[i].r-1;
        s[i] = c[k+t]*inv[k]%mod*inv[t]%mod;
        for(int j = 1;j < i;j++){
            k = node[i].c - node[j].c;
            t = node[i].r - node[j].r;
            if(k>=0&&t>=0)
            s[i] -= c[k+t]*inv[k]%mod*inv[t]%mod *s[j]%mod;
        }
        s[i] = (s[i]+mod)%mod;
    }
    s[n+1] = (s[n+1]+mod)%mod; 
    printf("%lld\n",s[n+1]);
    return 0;
}
View Code

 

 

 

Codeforces-559C Gerald and Giant Chess

原文:https://www.cnblogs.com/ljinggg/p/10683344.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!