计数类dp:

写在前面:
这一类dp特别要求不重不漏。统计对象的可能情况一般比较多,通常需要精确的划分和整体性计算。因此,使用动态规划抽象出问题中的子结构和推导的阶段,将有助于我们准确而高效的进行求解。

在求解计数类动态规划的问题时,通常要找到一个基准点,围绕这个基准点构造一个不可划分的整体,以避免子问题之间的重复。
一、Gerald and Giant Chess CodeForces - 559C:
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

题意:有一个h * w的棋盘,棋盘上有n个格子是黑色的,保证左上角和右下角是白色的。其他格子都是白色的,从左上角开始,可以向右走也可以向下走,但是不能走到黑色格子里面,求从左上角到右下角有多少种走法。
以看这题的数据量就知道关键点在黑色格子上。
我们设 最右下角的格子为第N+1个黑色格子。
设F(i) 为到达第 i 个黑色格子,并且途中不经过其他黑色格子的路线数,那么我们只需要求出 F(N+1)即可。
我们先把黑色格子按照行列的坐标递增排序。
则 F( i )= C(xi - 1 + yi - 1 ,xi - 1) - sum of F(j) * C(xi - xj + yi - yj , xi - xj)
其中 1 <= j <i ,xi >= xj,yi >=yj
上式表示,从 左上角到第 i 个黑色格子的总路径数,减去第一个经过的不是第 i 个黑色格子的路径数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#define ll long long
#define llu unsigned ll
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=200100;
struct node
{
   
    int x,y;
}a[maxn];

ll fac[maxn],inv_fac[maxn];
ll dp[maxn];

bool cmp(const node &a,const node &b)
{
   
    if(a.x!=b.x) return a.x<b.x;
    return a.y<b.y;
}

ll mypow(ll a,ll b)
{
   
    ll ans=1;
    while(b)
    {
   
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

void get_fac(void)
{
   
    int n=200000;
    fac[<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值