Codeforces Round #313 Gerald and Giant Chess (dp+组合数取模)

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

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

解析:这道题白点极多,黑点很少,应当从黑点着手。
      读入n个点,a[0]、a[1]。。。a[n-1],终点(h,w)即为a[n],然后以 x 为第一关键字,y为第二关键字从小到大排序。这样一来,能到达黑点 i 的黑点必然就在 i 点的前面。
      然后用 d[i] 表示不经过 i 前面的黑点,到达 i 点的方案数,显然,答案即为d[n]。
      对于每个点 i ,初始 d[i]=C(a[i].x-1+a[i].y-1,a[i].x-1),表示从点(1,1)到 a[i] 共有多少种方案。然后从 0 到 i-1 枚举 j 号点,若 j 号点能到达i 号点,则d[i]减去 从j到i的方案数*d[j]。

代码:
#include<cstdio>
#include<algorithm>
#define maxn1 100000
#define maxn2 2000
#define mod 1000000007
using namespace std;

int h,w,n;
long long f[maxn1*2+20],d[maxn2+20];
struct tnode{int x,y;}q[maxn2+20];

bool cmp(tnode a,tnode b)
{
  if(a.x<b.x)return 1;
  if(a.x>b.x)return 0;
  return a.y<b.y;
}

long long get(int x,int y)
{
  long long ans=1,a=(f[x]*f[y])%mod,b=mod-2;
  while(b>0)
    {
      if(b&1)ans=(ans*a)%mod;
      a=(a*a)%mod,b>>=1;
    }
  return f[x+y]*ans%mod;
}

int main()
{
  int i,j,k;
  scanf("%d%d%d",&h,&w,&n);
  f[0]=f[1]=1;
  for(i=2;i<h+w;i++)f[i]=(f[i-1]*i)%mod;
  for(i=0;i<n;i++)scanf("%d%d",&q[i].x,&q[i].y);
  sort(q,q+n,cmp);
  q[n].x=h,q[n].y=w;
  
  for(i=0;i<=n;i++)
    {
      d[i]=get(q[i].x-1,q[i].y-1);
      for(j=0;j<i;j++)if(q[j].x<=q[i].x && q[j].y<=q[i].y)
        d[i]=(d[i]-get(q[i].x-q[j].x,q[i].y-q[j].y)*d[j])%mod;  
    }
  while(d[n]<0)d[n]+=mod;
  printf("%I64d\n",d[n]);
  return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值