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

    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 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.

    Examples
    input
    3 4 2
    2 2
    2 3
    output
    2
    input
    100 100 3
    15 16
    16 15
    99 88
    output
    545732279
    分析:把不能走的格子和终点都设为要达到的目标;
       则要到达当前目标,就是从总方案数中减去从之前到达目标到达现在目标的方案数;
       预处理排序,阶乘,逆元即可;
       另外O(n)求1-n关于MOD的乘法逆元:inv[i] = ( MOD - MOD / i ) * inv[MOD%i] % MOD;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, rt<<1
    #define Rson mid+1, R, rt<<1|1
    const int maxn=2e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
    int n,m,k,t;
    ll fac[maxn],inv[maxn];
    void init()
    {
        fac[0]=inv[0]=fac[1]=inv[1]=1LL;
        for(int i=2;i<=maxn-10;i++)
        {
            fac[i]=fac[i-1]*i%mod;
            inv[i]=(mod-mod/i)*inv[mod%i]%mod;
        }
        for(int i=2;i<=maxn-10;i++)
            inv[i]=inv[i]*inv[i-1]%mod;
    }
    struct node
    {
        int x,y;
        ll num;
        bool operator<(const node&p)const
        {
            return x==p.x?y<p.y:x<p.x;
        }
    }a[maxn];
    ll gao(int x,int y)
    {
        int i,j;
        ll ans=1;
        ans=ans*fac[x+y-2]%mod;
        ans=ans*inv[x-1]%mod;
        ans=ans*inv[y-1]%mod;
        return ans;
    }
    int main()
    {
        int i,j;
        init();
        scanf("%d%d%d",&n,&m,&k);
        rep(i,0,k-1)scanf("%d%d",&a[i].x,&a[i].y);
        a[k].x=n,a[k].y=m;
        sort(a,a+k+1);
        rep(i,0,k)
        {
            a[i].num=gao(a[i].x,a[i].y);
            rep(j,0,i-1)a[i].num=(a[i].num-a[j].num*gao(a[i].x-a[j].x+1,a[i].y-a[j].y+1)%mod+mod)%mod;
        }
        printf("%lld
    ",a[k].num);
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    Python数据库查询中文乱码的问题处理
    Unable to update the EntitySet 'Relationship' because it has a DefiningQuery and no <DeleteFunction> element exists in the <ModificationFunctionMapping> element to support the current operation
    Entity Framework入门教程:通过Entity Framework实现数据库数据的增、删、改
    推荐几款.NET客户端开源报表图
    Entity Framework入门教程:SQLite数据源访问
    Entity Framework入门教程: Entity Framework支持的查询方式
    无法为具有固定名称“MySql.Data.MySqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6”
    Entity Framework入门教程:创建实体数据模型
    WPF's Style BasedOn
    在 WPF 中使用 Path 路径
  • 原文地址:https://www.cnblogs.com/dyzll/p/5901502.html
Copyright © 2011-2022 走看看