您的位置:首页 > 其它

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

2017-09-25 10:23 330 查看
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
;
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
, b
, dp
;

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
.x=h,p
.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
<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: