codeforces 560E Lucas定理+dp

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liyunlong41/article/details/52138955

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定理来求。


扫描二维码关注公众号,回复: 3744531 查看本文章

#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;
}



猜你喜欢

转载自blog.csdn.net/liyunlong41/article/details/52138955