传送门

解题思路

  暴力容斥复杂度太高,无法接受,考虑用\(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+组合数)的更多相关文章

  1. 51Nod 1486 大大走格子 —— 容斥

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1486 对于每个点,求出从起点到它,不经过其他障碍点的方案数: 求一 ...

  2. 51nod 1486 大大走格子——容斥

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1486 已知起点到某个障碍点左上角的所有点的不经过障碍的方案数,枚举 ...

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

    1486 大大走格子 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题   有一个h行w列的棋盘,里面有一些格子是不能走的,现在要 ...

  4. 51Nod 1486 大大走格子 —— 组合数学

    题目链接:https://vjudge.net/problem/51Nod-1486 1486 大大走格子 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: ...

  5. 51nod 1486 大大走格子——dp

    有一个h行w列的棋盘,里面有一些格子是不能走的,现在要求从左上角走到右下角的方案数. Input 单组测试数据. 第一行有三个整数h, w, n(1 ≤ h, w ≤ 10^5, 1 ≤ n ≤ 20 ...

  6. 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]- ...

  7. 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 ...

  8. [CF1086E]Beautiful Matrix(容斥+DP+树状数组)

    给一个n*n的矩阵,保证:(1)每行都是一个排列 (2)每行每个位置和上一行对应位置不同.求这个矩阵在所有合法矩阵中字典序排第几.考虑类似数位DP的做法,枚举第几行开始不卡限制,那么显然之前的行都和题 ...

  9. 【BZOJ3622】已经没有什么好害怕的了 容斥+DP

    [BZOJ3622]已经没有什么好害怕的了 Description Input Output Sample Input 4 2 5 35 15 45 40 20 10 30 Sample Output ...

随机推荐

  1. html基础与表格的理解·

    1.静态网页与动态网页的区别:是否访问数据库 2.超文本:超文本是指超出文本的范围,可以插入声音视频,表格图片等 3.标记语言与网页结构:标记语言就是标签,网页结构包含<html>< ...

  2. Spring cloud gateway自定义filter以及负载均衡

    自定义全局filter package com.example.demo; import java.nio.charset.StandardCharsets; import org.apache.co ...

  3. vue的概述

    一.Vue的概述 Vue的开发模式 和 之前接触的jQuery.原生JSDOM操作是不同的,之前要想修改试图,首先找元素:在使用Vue时,专心把精力放在修改数据.DOM驱动 ---> 数据驱动. ...

  4. Vue作用域插槽:基本用法

    一 项目结构 二 App组件 <template> <div id="app"> <!-- 子组件 --> <user v-slot:de ...

  5. poj1742Coins(多重背包)

    People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony ...

  6. Oracle基本操作练习(一)

    --创建表空间 create tablespace test datafile 'c:\test.dbf' size 100m autoextend on next 10m; --删除表空间 drop ...

  7. WPS for linux 中不能切换到中文输入法

    转载自:http://blog.sciencenet.cn/blog-200199-1032795.html 尽管安装有中文输入法,wps有时仍然不能切换到中文输入法,此问题解决方案如下: 根账户下打 ...

  8. 《JAVA设计模式》之访问者模式(Visitor)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述访问者(Visitor)模式的: 访问者模式是对象的行为模式.访问者模式的目的是封装一些施加于某种数据结构元素之上的操作.一旦这些操作需要 ...

  9. pgsql删除重复记录

    如下: DELETE FROM categories a WHERE ( a.id, a.name, a.parent_id ) ) ) 关键点:oracle中有内部id为rowid, 在postgr ...

  10. "源文件名长度大于文件系统支持的长度无法删除"的解决方案

    import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * @auth ...