codeforces559C Gerald and Giant Chess(计数dp)

Gerald and Giant Chess

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   ≤   1 0 5 ,   1   ≤   n   ≤   2000 ) (1 ≤ h, w ≤ 10^5, 1 ≤ n ≤ 2000) (1h,w105,1n2000).

Next n n n lines contain the description of black cells. The i-th of these lines contains numbers r i ,   c i ( 1   ≤   r i   ≤   h ,   1   ≤   c i   ≤   w ) r_i, c_i (1 ≤ r_i ≤ h, 1 ≤ c_i ≤ w) ri,ci(1rih,1ciw) — 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 1 0 9   +   7 10^9 + 7 109+7.

思路

将黑色方块按照x坐标,y坐标递增排序。f[i]代表从左上角出发,第一次在 x i , y i x_i,y_i xi,yi这个位置踩到黑色方块,可能的路径数量。

于是有状态转移方程:
f [ i ] = C h i + w i − 2 h i − 1 − ∑ j = 1 j = i − 1 f [ j ] × C h i + w i − h j − w j h i − h j f[i]=C^{h_i-1}_{h_i+w_i-2}-\sum_{j=1}^{j=i-1}f[j]\times C^{h_i-h_j}_{h_i+w_i-h_j-w_j} f[i]=Chi+wi2hi1j=1j=i1f[j]×Chi+wihjwjhihj
设第 0 0 0个黑色方块为 { 1 , 1 } \{1,1\} {1,1},第 n + 1 n+1 n+1个黑色方块为 { h , w } \{h,w\} {h,w},那么所求答案为 f [ n + 1 ] f[n+1] f[n+1]

可以先预处理出所有的阶乘,以及阶乘的逆元,再进行计算。

#include<cstdio>
//#include<utility>
#include<algorithm>
#define int long long
#define x first
#define y second
using namespace std;
const int mod=1e9+7;
int h,w,n,jc[200010],jcinv[200010],f[2010];
pair<int,int> a[2010];

int qpow(int a,int b){
    int ret=1;
    while(b){
        if(b&1) ret=ret*a%mod;
        b>>=1,a=a*a%mod;
    }
    return ret;
}

int C(int up,int down){
    return jc[down]*jcinv[up]%mod*jcinv[down-up]%mod;
}

signed main(){
    jc[0]=1;    jcinv[0]=1; f[0]=1;
    for(int i=1;i<=200000;i++){
        jc[i]=i*jc[i-1]%mod;
        jcinv[i]=qpow(jc[i],mod-2);
    }
    scanf("%lld%lld%lld",&h,&w,&n);
    for(int i=1;i<=n;i++)   scanf("%lld%lld",&a[i].x,&a[i].y);
    sort(a+1,a+n+1);
    a[n+1].x=h,a[n+1].y=w;
    for(int i=1;i<=n+1;i++){
        f[i]=C(a[i].x-1,a[i].x+a[i].y-2);
        for(int j=1;j<i;j++){
            if(a[j].y>a[i].y)   continue;
            f[i]=(f[i]-f[j]*C(a[i].x-a[j].x,a[i].x+a[i].y-a[j].x-a[j].y))%mod;
        }
    }
    printf("%lld\n",(f[n+1]+mod)%mod);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_51864047

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值