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

    C. 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
    3 4 2
    2 2
    2 3
    Output
    2
    Input
    100 100 3
    15 16
    16 15
    99 88
    Output
    545732279

    总路径数减去经过黑点的路径数就是答案。若之前无障碍,走到点(x,y)的路径数=C(x,x+y)。实际计算时为避免重复,要减去从之前的黑点到当前黑点的路径数。

    计算的值非常大,由于涉及取模除法,需要用到乘法逆元算法。

    乘法逆元什么鬼!虽然看懂了但是不会实现,我选择死亡

    高端解析和高端代码链接:http://blog.csdn.net/sdfzyhx/article/details/51822192

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<cstring>
     6 using namespace std;
     7 const int mxn=200200;
     8 const long long p=1000000007;
     9 long long jc[mxn],rc[mxn];//阶乘 逆元 
    10 long long dp[2005];
    11 int h,w,n;
    12 struct node{
    13     int x,y;
    14 }a[2005];
    15 int cmp(node a,node b){
    16     if(a.x!=b.x)return a.x<b.x;
    17     return a.y<b.y;
    18 }
    19 void eu(long long a,long long b,long long &x,long long &y){//扩展欧几里得 
    20     if(!b){
    21         x=1;y=0;
    22     }
    23     else{
    24         eu(b,a%b,y,x);
    25         y-=x*(a/b);
    26     }
    27     return;
    28 }
    29 void init(){
    30     int i;
    31     jc[0]=rc[0]=1;
    32     long long x;
    33     for(i=1;i<200005;i++){
    34         jc[i]=(jc[i-1]*i)%p;//阶乘
    35         eu(jc[i],p,rc[i],x);
    36         rc[i]=(rc[i]%p+p)%p;
    37     }
    38     return;
    39 }
    40 long long c(int x,int y){
    41     return jc[y]*rc[x]%p*rc[y-x]%p;
    42 }
    43 int main(){
    44     init();
    45     scanf("%d%d%d",&h,&w,&n);
    46     int i,j;
    47     for(i=1;i<=n;i++){
    48         scanf("%d%d",&a[i].x,&a[i].y);
    49     }
    50     n++;a[n].x=h;a[n].y=w;
    51     sort(a+1,a+n+1,cmp);
    52     for(i=1;i<=n;i++){
    53         dp[i]=c(a[i].x-1,a[i].x+a[i].y-2);
    54 //        printf("---%d---
    ",dp[i]);
    55         for(j=1;j<i;j++){
    56             if(a[j].y<=a[i].y)
    57                 dp[i]=((dp[i]-dp[j]*c(a[i].x-a[j].x,a[i].x+a[i].y-a[j].x-a[j].y)%p)+p)%p;//减去重复值 
    58         }
    59     }
    60     printf("%lld
    ",dp[n]);
    61     return 0;
    62 }
  • 相关阅读:
    kubectl exec 执行 容器命令
    centos下通过yum安装redis-cli
    windows下 使用ip地址反查主机名的命令
    O365(世纪互联)SharePoint 之文档库使用小记
    SharePoint 2016 图文安装教程
    SharePoint 2013 激活标题字段外的Menu菜单
    SharePoint 2013 定制搜索显示模板(二)
    SharePoint 2013 定制搜索显示模板
    SharePoint 2013 网站搜索规则的使用示例
    SharePoint 2013 搜索功能,列表项目不能完全被索引
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5642674.html
Copyright © 2011-2022 走看看