51nod 1486 大大走格子(容斥+dp+组合数)
解题思路
暴力容斥复杂度太高,无法接受,考虑用\(dp\)。设\(f(i)\)表示从左上角开始不经过前面的阻断点,只经过\(i\)的阻断点。那么可以考虑容斥,用经过\(i\)的总方案数减去前面的阻断点到它的方案数,那么转移方程$$f(i)=C(x_i+y_i-2,x_i)-\sum\limits_{j=1}^{i-1}f(j)C(x_i-x_j,y_i-y_j)$$
时间复杂度\(O(n^2)\)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=200005;
const int M=2005;
const int MOD=1e9+7;
inline int rd(){
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return f?x:-x;
}
int h,w,n,f[M],fac[N],inv[N];
struct Node{
int x,y;
friend bool operator<(const Node A,const Node B){
return A.x==B.x?A.y<B.y:A.x<B.x;
}
}node[M];
inline int fast_pow(int x,int y){
int ret=1;
for(;y;y>>=1){
if(y&1) ret=1ll*ret*x%MOD;
x=1ll*x*x%MOD;
}
return ret;
}
inline void prework(){
fac[0]=1; inv[0]=1;
for(int i=1;i<=h+w;i++) fac[i]=1ll*fac[i-1]*i%MOD;
inv[h+w]=fast_pow(fac[h+w],MOD-2);
for(int i=h+w-1;i;i--) inv[i]=1ll*inv[i+1]*(i+1)%MOD;
}
inline int C(int x,int y){
if(y<0) return 0;
return 1ll*fac[x]*inv[y]%MOD*inv[x-y]%MOD;
}
int main(){
h=rd(),w=rd(),n=rd(); prework();
for(int i=1;i<=n;i++)
node[i].x=rd(),node[i].y=rd();
node[++n].x=h,node[n].y=w;
sort(node+1,node+1+n); int x,y;
for(int i=1;i<=n;i++){
x=node[i].x,y=node[i].y;
f[i]=C(x-1+y-1,y-1);
for(int j=1;j<i;j++)
(f[i]-=1ll*f[j]*C(x-node[j].x+y-node[j].y,y-node[j].y)%MOD)%=MOD;
f[i]=(f[i]+MOD)%MOD;
}
printf("%d\n",f[n]);
return 0;
}
51nod 1486 大大走格子(容斥+dp+组合数)的更多相关文章
- 51Nod 1486 大大走格子 —— 容斥
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1486 对于每个点,求出从起点到它,不经过其他障碍点的方案数: 求一 ...
- 51nod 1486 大大走格子——容斥
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1486 已知起点到某个障碍点左上角的所有点的不经过障碍的方案数,枚举 ...
- 51nod 1486 大大走格子(容斥原理)
1486 大大走格子 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 有一个h行w列的棋盘,里面有一些格子是不能走的,现在要 ...
- 51Nod 1486 大大走格子 —— 组合数学
题目链接:https://vjudge.net/problem/51Nod-1486 1486 大大走格子 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: ...
- 51nod 1486 大大走格子——dp
有一个h行w列的棋盘,里面有一些格子是不能走的,现在要求从左上角走到右下角的方案数. Input 单组测试数据. 第一行有三个整数h, w, n(1 ≤ h, w ≤ 10^5, 1 ≤ n ≤ 20 ...
- 51nod 1486 大大走格子(DP+组合数学)
枚举不合法点的思想. 把障碍x坐标为第一关键字,y坐标为第二关键字排序.f[i]表示走到第i个障碍的方案数. f[i]=C(x[i]+y[i]-2,x[i]-1)-sigma(f[j]*C(x[i]- ...
- HDU 5794 A Simple Chess (容斥+DP+Lucas)
A Simple Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5794 Description There is a n×m board ...
- [CF1086E]Beautiful Matrix(容斥+DP+树状数组)
给一个n*n的矩阵,保证:(1)每行都是一个排列 (2)每行每个位置和上一行对应位置不同.求这个矩阵在所有合法矩阵中字典序排第几.考虑类似数位DP的做法,枚举第几行开始不卡限制,那么显然之前的行都和题 ...
- 【BZOJ3622】已经没有什么好害怕的了 容斥+DP
[BZOJ3622]已经没有什么好害怕的了 Description Input Output Sample Input 4 2 5 35 15 45 40 20 10 30 Sample Output ...
随机推荐
- 【经验分享】 解决CentOS7 安装VMTools提示找不到Kernel头文件的方案
配置一个Linux的开发环境,用VM10+CentOS7(Kernel版本3.10.0-327.10.1.el7),之后发现VMTools功能不全,查证后发现需要卸载重装,于是开始折腾. 按照各种说明 ...
- 【The type javax.servlet.http.HttpServletRequest cannot be resolved】解决方案
是缺少serverlet的引用库,解决如下 1.工程右键-properties->java build path 2.在java build path的libraries tab页中选择Add ...
- Java thread(4)
这一块主要是讨论关于进程同步的相关问题,主要是考虑一下的关键字:锁对象.条件对象 -> synchronized wait() notify(). 1.关于锁对象与条件对象: 所对象的定义在ja ...
- app本身性能测试简介
app 性能测试指标: 1.启动时间 2.内存占用量,内存警告次数 3.页面渲染时间,刷新帧率 4.网络请求时间.流量消耗 5.UI阻塞次数,不可操作时长,主线程阻塞超过400毫秒次数 6.耗电功率 ...
- MySQL-第一篇认识MySQL
1.什么是mysql mysql是一种关系型数据库,是瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品. 2.mysql的安装 下载mysql-installer-community- ...
- Spring Boot 1.x 正式退役,2.x大步向前!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 早在<Spring Boot 2.1.5 正式发布,1.5.x 即将结束使命!>一文中栈长就提醒大家 Sprin ...
- 【洛谷p1217】回文质数
回文质数[题目链接] 始终要记得凌云壮flag(真香) 说是个搜索,其实感觉更像是一个暴力: 这个题的难度并不是特别大,因为下面的提示太明显了qwq,(而且之前培训也讲过)首先是构造回文数,构造回文数 ...
- ZOJ 2706 Thermal Death of the Universe
Thermal Death of the Universe Time Limit: 10 Seconds Memory Limi ...
- K The Right-angled Triangles
链接:https://ac.nowcoder.com/acm/contest/338/K来源:牛客网 题目描述 Consider the right-angled triangles with sid ...
- 【接口工具】接口抓包工具之Charles
上篇我们讲了Fiddler,Fiddler是用C#开发的,所以Fiddler不能在Mac系统中运行,没办法直接用Fiddler来截获MAC系统中的HTTP/HTTPS, Mac 用户怎么办呢? 1.F ...