codeforces 560E Lucas定理+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.

Examples
input
3 4 2
2 2
2 3
output
2
input
100 100 3
15 16
16 15
99 88
output
545732279

题意:求从1,1到n,m的方法数,每次可以从当前的点向右边移动一格,或者向下移动一格,但是有的格子是坏的,不能经过这些点。

分析:考虑dp来做,把坏的点排序,dp[i]表示到第i个坏的点但是不经过其他坏点有多少种方法,最后添加一个坏点n,m正好最后求dp【k】就是答案,统计dp[i]的过程中先算出从1,1到这点的组合数,然后减去x跟y都要比当前的点小的坏点到这个点的组合数,由于组合数中n,m都比较大,因此要用lucas定理来求。


#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;char c;int f=1;
    while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res*f;
}
const int N=100010,MOD=1e9+7;

ll dp[2345],fac[N<<1];

ll power(ll a,ll n,ll p)
{
    ll ret=1;
    while(n)
    {
        if(n & 1) ret = ret*a%p;
        a = a*a%p;
        n >>= 1;
    }
    return ret;
}

ll C(ll n,ll m, ll p)
{
    if(m>n) return 0;
    return fac[n]*power(fac[m]*fac[n-m]%p,p-2,p)%p;
}

ll Lucas(ll n,ll m,ll p)
{
    if(m==0) return 1;
    return C(n%p,m%p,p)*Lucas(n/p,m/p,p)%p;
}
pii a[2345];
int main()
{
    fac[0]=1;
    for(int i=1;i<N<<1;i++){  //
        fac[i] = fac[i-1]*i%MOD;
    }
    int n=in(),m=in(),k=in();
    for(int i=0;i<k;i++){
        a[i].F=in(),a[i].S=in();
    }
    a[k].F=n,a[k].S=m;

    sort(a,a+k);

    for(int i=0;i<=k;i++){
        dp[i] = Lucas(a[i].F-1+a[i].S-1,a[i].F-1,MOD);
        for(int j=0;j<i;j++){
            if(a[j].S > a[i].S) continue;
            dp[i] = (dp[i]-Lucas(a[i].F-a[j].F+a[i].S-a[j].S,a[i].F-a[j].F,MOD)*dp[j]+MOD)%MOD;
        }
    }
    dp[k] += MOD;
    dp[k] %= MOD;
    cout<<dp[k]<<endl;
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值