zoukankan      html  css  js  c++  java
  • E. Gerald and Giant Chess

    E. Gerald and Giant Chess
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes2015-09-09
    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 anh × 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 modulo109 + 7.

    Sample test(s)
    input
    3 4 2
    2 2
    2 3
    output
    2
    input
    100 100 3
    15 16
    16 15
    99 88
    output
    545732279

    我们对着2000个点离(1,1)点的距离进行排序;
    dp[i] 表示 从11点没有经过黑点到达i的方案数
    dp[i]=C(x+y-2,y-1)-(dp[j]*C(x-x1+y-y1,x-x1))
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string.h>
    #include <vector>
    using namespace std;
    const int maxn=2000+10;
    typedef long long LL;
    const LL mod=1000000007;
    LL dp[maxn];
    struct point{
       int x,y;
       bool operator < (const point &rhs) const
       {
            return (x+y)<(rhs.x+rhs.y);
       }
    }P[maxn];
    LL fax[100005*2];
    void init()
    {
       int n=100000*2;
       fax[0]=1;
       for(int i=1; i<=n; i++)
         fax[i]=(fax[i-1]*i)%mod;
    }
    void gcd(LL a, LL b, LL &d, LL &x, LL &y)
    {
         if(b==0){
            d=a; x=1;y=0;
         }else {
            gcd(b,a%b,d,y,x); y-=x*(a/b);
         }
    }
    LL inv(LL a,LL n)
    {
         LL d,x,y;
         gcd(a,n,d,x,y);
         return (x+n)%n;
    }
    LL lucas(int n,int m)
    {
        LL ans=1;
         while(n&&m)
            {
                int a=n%mod,b=m%mod;
                if(a<b)return 0;
                ans= ( ( ( ans * fax[a] )%mod )* inv(fax[b]*fax[a-b],mod) ) %mod;
                n/=mod ; m/=mod;
            }
            return ans;
    }
    int main()
    {
         int h,w,n;
         init();
         while(scanf("%d%d%d",&h,&w,&n)==3)
         {
             for(int i=0; i<n; i++)
                {
                    scanf("%d%d",&P[i].x,&P[i].y);
                }
             P[n].x=h;P[n].y=w;
             sort(P,P+n+1);
    
             for(int i=0; i<=n; i++)
                {
                    dp[i]=lucas(P[i].x+P[i].y-2,P[i].y-1);
                    for(int j=0; j<i; j++)
                       if(P[i].x>=P[j].x&&P[i].y>=P[j].y)
                        {
                             dp[i]= ( ( dp[i] - ( dp[j]*lucas(P[i].x-P[j].x+P[i].y-P[j].y ,P[i].x-P[j].x ) )%mod)+mod)%mod;
                        }
                }
                printf("%I64d
    ",dp[n]);
         }
    
        return 0;
    }
    View Code

    2015-09-09---opas





  • 相关阅读:
    1022.游船出租
    1021.统计字符
    1020.最小长方形
    1017.还是畅通工程
    1019.简单计算器
    1015.还是A+B
    1014.排名
    1013.开门人和关门人
    1011.最大连续子序列
    1009.二叉搜索树
  • 原文地址:https://www.cnblogs.com/Opaser/p/4794161.html
Copyright © 2011-2022 走看看