Gerald and Giant Chess (CodeForces - 560E)(组合数)

本文探讨了一种特殊棋盘游戏——巨棋中的最优路径寻找问题。游戏目标是将卒子从棋盘左上角移动到右下角,过程中避免经过黑色方格。文章通过组合数学与动态规划的方法,计算出所有可能的胜利路径数量,并给出具体实现代码。
ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

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.

Example
Input
3 4 2
2 2
2 3
Output
2
Input
100 100 3
15 16
16 15
99 88
Output
545732279

#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<string>  
#include <algorithm>  
#include<cmath>
#include<vector>
#define mod 1000000007
using namespace std;

long long fac[200005],inv[200005];
long long sum[2005];
int h,w;
long long pow(long long x,int k)
{
    long long ans=1;
    while(k)
    {
        if(k&1)
            ans=ans* x%mod;
        x=x*x%mod;
        k>>=1;
    } 
    return ans;
} 
void init()
{
    fac[0]=inv[0]=1;
    for(int i=1;i<=h+w;i++)
        fac[i]=fac[i-1]*i%mod;
    int c=max(h,w);
     inv[c]=pow(fac[c],mod-2);
     for(int i=c-1;i>=1;i--)
        inv[i]=inv[i+1]*(i+1)%mod;   
}
struct node
{
    int x,y;
};
int cmp(node a,node b)
{
    return a.x<b.x||(a.x==b.x&&a.y<b.y); 
} 
node p[2005];
int main()  
{  
    int n;
    scanf("%d%d%d",&h,&w,&n);
    init();
    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,cmp);

    int x1,y1,x2,y2;
    for(int i=0;i<n;i++)
    {
        x1=p[i].x-1;
        y1=p[i].y-1;
        sum[i]=fac[x1+y1]*inv[x1]%mod*inv[y1]%mod; 
        for(int j=0;j<i;j++)
        {
            if(p[j].x<=p[i].x&&p[j].y<=p[i].y)
            {
                x2=x1-p[j].x+1;
                y2=y1-p[j].y+1;
                sum[i]=(sum[i]-fac[x2+y2]*inv[x2]%mod*inv[y2]%mod*sum[j] %mod)%mod;
                if(sum[i]<0)sum[i]=(sum[i]+mod)%mod;
            } 
        }
    } 
     printf("%lld\n",sum[n-1]);
    return 0;  
} 

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值