codeforces 560E Gerald and Giant Chess (dp + 组合数)

33 篇文章 1 订阅
10 篇文章 0 订阅

题目原文:http://codeforces.com/contest/560/problem/E

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


题目大意:有一个n * m的棋盘,中间有不多于2000个点被选定,不能走,问从左上角到右下角有多少种走法?


解题思路:

首先考虑没有不能走的方格的情况,这就变成了高中组合数学的问题,C(h+w-2,h-1)

再考虑有一个不能走的方格的情况,需要用上面的值减去经过这个方格的情况(也就是C(x+y-2,x-1)*C(h-x+w-y,h-x))

所以我们考虑向多个不能走的方格推广。

我们定义 f[i] 为从左上角到这里经过前面那些点干扰的情况下有多少种走法,转移的办法就是每次减去在(x,y)这个矩形里面的点对后面点的干扰。


AC代码:

/*
    @Author: wchhlbt
    @Date:   2017/5/11
*/
#include <bits/stdc++.h>

#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 200022
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

struct Black{
    ll x,y;
}b[2400];
ll f[maxn],inv[maxn],dp[2005];
bool cmp(Black r, Black s)
{
    if(r.x==s.x)    return r.y < s.y;
    return r.x < s.x;
}

ll quickpow(ll m,ll n,ll k)     // return m^n % k
{
    ll b = 1;
    m = m%k;
    while (n > 0)
    {
          if (n & 1)
             b = (b*m)%k;
          n = n >> 1 ;
          m = (m*m)%k;
    }
    return b;
}

ll C(ll n, ll m)
{
    return (((f[n] * inv[m])%mod) * inv[n-m]) % mod;
}

void init()
{
    f[0] = 1;
    for(int i = 1; i<=maxn-2; i++)
        f[i] = (i*f[i-1])%mod;
    inv[maxn-2] = quickpow(f[maxn-2],mod-2,mod);
    for(int i = maxn - 3; i>=0; i--)
        inv[i] = ( (i+1) * inv[i+1] ) % mod;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //cout << inv[0] << endl;
    init();
    ll h,w,n;
    scanf("%I64d%I64d%I64d",&h,&w,&n);
    for(int i = 0; i<n; i++){
        scanf("%I64d%I64d",&b[i].x, &b[i].y);
    }
    sort(b,b+n,cmp);
    b[n].x = h; b[n].y = w;
    for(int i = 0; i<=n; i++){
        dp[i] = C( b[i].x + b[i].y - 2, b[i].x - 1);
        for(int j = 0; j<i; j++){
            if(b[j].y<=b[i].y){
                dp[i] = (dp[i] - dp[j] * C(b[i].x + b[i].y - b[j].x - b[j].y, b[i].x - b[j].x))%mod;
                dp[i] = (dp[i] + mod)%mod;
            }
        }
    }
    printf("%I64d\n",dp[n]);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
区间DP是一种动态规划的方法,用于解决区间范围内的问题。在Codeforces竞赛中,区间DP经常被用于解决一些复杂的字符串或序列相关的问题。 在区间DP中,dp[i][j]表示第一个序列前i个元素和第二个序列前j个元素的最优解。具体的转移方程会根据具体的问题而变化,但是通常会涉及到比较两个序列的元素是否相等,然后根据不同的情况进行状态转移。 对于区间长度为1的情况,可以先进行初始化,然后再通过枚举区间长度和区间左端点,计算出dp[i][j]的值。 以下是一个示例代码,展示了如何使用区间DP来解决一个字符串匹配的问题: #include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> using namespace std; const int maxn=510; const int inf=0x3f3f3f3f; int n,dp[maxn][maxn]; char s[maxn]; int main() { scanf("%d", &n); scanf("%s", s + 1); for(int i = 1; i <= n; i++) dp[i][i] = 1; for(int i = 1; i <= n; i++) { if(s[i] == s[i - 1]) dp[i][i - 1] = 1; else dp[i][i - 1] = 2; } for(int len = 3; len <= n; len++) { int r; for(int l = 1; l + len - 1 <= n; l++) { r = l + len - 1; dp[l][r] = inf; if(s[l] == s[r]) dp[l][r] = min(dp[l + 1][r], dp[l][r - 1]); else { for(int k = l; k <= r; k++) { dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]); } } } } printf("%d\n", dp[n]); return 0; } 希望这个例子能帮助你理解区间DP的基本思想和应用方法。如果你还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值