11 , 路径的条数数据较小(o(n2))的暴力,还可以采用组合数的方式求出来(Lucas)(log(n))的时间复杂度

假设从一个矩阵的左上角的(1,1)位置走到右下角(n,m)的位置  只能往右走或是往下走;

方法1 :    暴力dp[i][j]=dp[i-1][j]+dp[i][j-1];  dp[n][m];

方法2:  总的来看的话从左上角走到右下角必须要走n+m-2步,其中n-1步必须要是往下走,m-1步必须是往右面走,这个问题就像是

              从n+m个物品中选出n个物品来,剩下的物品就随便了C(n+m-2,n-1),这样的话就一定要用到Lucas定理了;

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

  对于这道题目,我们可以先看第i个坏点的情况,ff[i]表示的是到第i个点的时候合法路径,那么这个地方的合法的条数

  就是  从起点到这个点的所有路径条数减掉前面每一个坏点的的合法条数乘上到这个点到点的所有可能路径条数

  坏点   1                2             3                    4

        2  2            2  3   

 -以坏1开头    -以坏2开头   类似

      的排列      的排列

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,s,n) for(ll i=s;i<=n;i++)
#define per(i,n,s) for(ll i=n;i>=s;i--)
const int Max = 2e5+10;
const int maxn = 2e5+10;
const ll mod  = 1e9+7;
struct node
{
    ll x,y;
}p[Max];
//ll dp[60][60];
//void Init(){
//    rep(i,1,50){
//       dp[i][1]=1;
//       dp[1][i]=1;
//    }
//    rep(i,2,50){
//       rep(j,2,50){
//          dp[i][j]=dp[i-1][j]+dp[i][j-1];
//          dp[i][j]=(dp[i][j]%mod+mod)%mod;
//       }
//    }
//}//暴力验证情况
bool cmp(node a, node b){
   if(a.x!=b.x){
    return a.x<b.x;
   }
   return a.y<b.y;
}
ll c[maxn];
ll f[maxn];
ll qmod(long long a, long long b)
{
    ll res = 1ll;
    while(b)
    {
        if(b&1)
        {
            res=res*a%mod;
        }
        b>>=1;
        a=a*a%mod;
    }
    return res;
}
ll inv[maxn];
void init()
{
    f[0]=1;
    for(ll i=1;i<maxn;i++)
    {
        f[i]=(f[i-1]*i)%mod;
    }
    inv[maxn-1]=qmod(f[maxn-1],mod-2);
    for(ll i=maxn-2;i>=0;i--)
    {
        inv[i]=(inv[i+1]*(i+1))%mod;
    }
}
ll Lucas(long long n, long long m)
{
    return f[n]*inv[m]%mod*inv[n-m]%mod;
}
ll ff[Max];
int main(){
     ll h,w,n;
     init();
//     Init();
//     while(1){
//         scanf("%lld %lld",&h,&w);
//         printf("%lld\n",Lucas(h+w-2,h-1));
//         printf("%lld\n",dp[h][w]);
//     }//暴力验证的情况
     scanf("%lld %lld %lld",&h,&w,&n);
     rep(i,1,n){
       scanf("%lld %lld",&p[i].x,&p[i].y);
    }
    sort(p+1,p+1+n,cmp);
    ll sum=Lucas(h+w-2,h-1);
    printf("%lld\n",sum);
    rep(i,1,n){
       ff[i]=Lucas(p[i].x+p[i].y-2,p[i].x-1);
       ff[i]=(ff[i]%mod+mod)%mod;
       rep(j,1,i-1){
          ff[i]=(ff[i]-ff[j]*Lucas((p[i].x-p[j].x+p[i].y-p[j].y),(p[i].x-p[j].x)));
          ff[i]=(ff[i]%mod+mod)%mod;
       }
       sum=(sum-ff[i]*Lucas((h-p[i].x+w-p[i].y),(h-p[i].x)));
       sum=(sum%mod+mod)%mod;
    }
    printf("%lld\n",sum);
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值