51 nod 1486 大大走格子(容斥原理)

题目来源:  CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 160  难度:6级算法题
 收藏
 关注

有一个h行w列的棋盘,里面有一些格子是不能走的,现在要求从左上角走到右下角的方案数。


Input
单组测试数据。
第一行有三个整数h, w, n(1 ≤ h, w ≤ 10^5, 1 ≤ n ≤ 2000),表示棋盘的行和列,还有不能走的格子的数目。
接下来n行描述格子,第i行有两个整数ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w),表示格子所在的行和列。
输入保证起点和终点不会有不能走的格子。
Output
输出答案对1000000007取余的结果。
Input示例
3 4 2
2 2
2 3
Output示例
2


解:ans=从左上角到到终点所有的方案数-经过一个黑点的方案数+经过两个黑点的方案数-3+4.。。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
const int N = 1e6+10;
typedef long long LL;
const LL mod = 1000000007;
struct node
{
    LL x, y;
    bool operator <(const node &A)const
    {
        if(x!=A.x) return x<A.x;
        else return y<A.y;
    }
}p[N];
LL quick(LL x,LL n)
{
    LL r=1;
    while(n)
    {
        if(n&1) r=r*x%mod;
        n>>=1;
        x=x*x%mod;
    }
    return r;
}
LL c[N], b[N], dp[N];

int main()
{
    c[0]=1,b[0]=1;
    for(LL i=1;i<=400000+10;i++)
    {
        c[i]=c[i-1]*i%mod;
        b[i]=quick(c[i],mod-2);
    }
    LL h, w, n;
    scanf("%lld %lld %lld", &h, &w, &n);
    for(int i=0;i<n;i++)
        scanf("%lld %lld", &p[i].x, &p[i].y);
    p[n].x=h,p[n].y=w;
    sort(p,p+n+1);
    for(int i=0;i<=n;i++)
    {
        LL x=p[i].x,y=p[i].y;
        dp[i]=c[x+y-2]*b[x-1]%mod*b[y-1]%mod;
        for(int j=0;j<i;j++)
        {
            int x1=p[j].x, y1=p[j].y;
            if(x>=x1&&y>=y1)
            {
                LL x2=x-x1+1, y2=y-y1+1;
                LL cnt=c[x2+y2-2]*b[x2-1]%mod*b[y2-1]%mod;
                dp[i]=((dp[i]-(dp[j]*cnt)%mod)%mod+mod)%mod;
            }
        }
    }
    cout<<dp[n]<<endl;
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值