Description

Yehan is a angry grumpy rabbit, who likes jumping into the hole. This day,Yehan jumps again in the pit at home. Each time, he should jump from the hole at the coordinate (1,1) to (n,m), and he has to jump as the way : he can only jump (x, y) to (x+1, y) or (x, y+1). At his home, some holes are filled with water, he couldn’t jump in them. Yehan wants to know how many different way he could jump. If the paths of two ways are different, Yehan considers they are different.

Input

Multiple test dataThe first line of input contains two numbers n, m (2 <= n, m < 1e5)The second line of input contains a number q, the number of holes (q <= 15)The following q lines, the i-th line contains two numbers xi, yi (1 < x1 < x2 < x3 <……< xq < n) (1 < y1 < y2 < y3 < …… < yq < m)

Output

Output contains one line, the number of ways, because the result is too large, you should mod 1000000007

题意:从(1,1)走到(n,m),其中有些格子有水坑不能走(保证水坑坐标(1 < x1 < x2 < x3 <……< xq < n) (1 < y1 < y2 < y3 < …… < yq < m),之前没看到这个坑死),问你有多少路径。

思路:对于x*y的方格,从左上走到右下的路径数为:Cxx+y。这个高中组合应该讲过,因为横向必走x步,而且只要这x步确定了那么纵向怎么走必确定(可以自己试试),而横着走有x + y种可能。

所以这道题我们要做的是总路径减去走水坑的路径。走水坑路径和总路径做法一样,但要用容斥。因为这里要组合数取模1e+7,所以还要用到逆元。一开始很傻比直接算逆元,其实这里直接打表阶乘的逆元,具体看下面参考内容。

代码最后是数据。

参考:【逆元】

代码:

#include<cstdio>
#include<set>
#include<vector>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = +;
const double INF = 0x3f3f3f3f;
const ll MOD = ;
struct node{
int x, y;
}p[];
ll ans, n, m;
ll fac[maxn << ], inv[maxn << ];
int q;
int cmp(node a, node b){
return a.x == b.x? a.y < b.y : a.x < b.x;
}
ll pmul(ll a, ll b){
ll ans = ;
while(b){
if(b & ) ans = (ans * a) % MOD;
a = a * a % MOD;
b >>= ;
}
return ans;
}
void init(){ //阶乘的逆元
fac[] = fac[] = ;
for(ll i = ; i <= ; i++)
fac[i] = fac[i - ] * i % MOD;
inv[] = pmul(fac[], MOD - );
for(ll i = - ; i >= ; i--)
inv[i] = inv[i + ] * (i + ) % MOD;
}
ll C(ll x, ll y){ //组合逆元
return (fac[y] * inv[y - x] % MOD) * inv[x] % MOD;
}
void dfs(int id, int pre, ll temp, int times){
ll tmp1, tmp2;
times++;
tmp1 = (temp * C(p[id].x - p[pre].x, p[id].x - p[pre].x + p[id].y - p[pre].y)) % MOD;
tmp2 = (tmp1 * C(n - p[id].x, n - p[id].x + m - p[id].y)) % MOD;
if(times & ) ans = (ans + tmp2) % MOD;
else ans = (ans - tmp2) % MOD;
for(int i = id + ; i <= q; i++){
dfs(i, id, tmp1, times);
}
}
int main(){
init();
while(~scanf("%lld%lld", &n, &m)){
scanf("%d", &q);
p[].x = , p[].y = ;
for(int i = ; i <= q; i++){
scanf("%d%d", &p[i].x, &p[i].y);
}
ans = ;
for(int i = ; i <= q; i++){
dfs(i, , , ); //now pre mul time
}
printf("%lld\n", ((C(n - , n + m - ) - ans) % MOD + MOD) % MOD);
}
return ;
} /*
input
6122 61753
2
1957 18165
4448 30108
91557 89797
9
11169 5062
30315 34046
37127 36827
44369 50521
62770 58297
64008 62857
79378 67405
88132 86222
89483 87524
74355 52758
12
1193 7922
11316 9175
19053 11037
38189 11344
38317 15083
40095 19756
41161 24874
47120 30594
50188 34496
51327 36374
53291 50539
66554 51069
56766 19695
7
2012 1210
12159 11135
18759 13249
40035 13828
41494 13959
45882 18241
49535 19379
31178 59737
7
698 14597
7306 14856
8198 19316
9000 45862
10879 53006
12002 54423
24634 58820
81872 8035
4
32720 1451
40464 4253
51261 6151
72014 6683
21393 42250
13
422 1192
478 2683
3265 3684
4422 4366
4760 13041
6586 22349
6803 24676
7273 25162
8875 29191
13875 32524
14791 33611
17168 34162
21102 39838
5572 26973
9
1760 697
2106 4583
2141 6788
2438 6790
3811 7301
4293 11943
4607 15554
5164 15929
5529 18282 output
390661224
872660150
97529549
252410050
742624594
697140589
674450439
109291758
*/

FJNUOJ Yehan’s hole(容斥求路径数 + 逆元)题解的更多相关文章

  1. HDU-2204-Eddy's爱好-容斥求n以内有多少个数形如M^K

    HDU-2204-Eddy's爱好-容斥求n以内有多少个数形如M^K [Problem Description] 略 [Solution] 对于一个指数\(k\),找到一个最大的\(m\)使得\(m^ ...

  2. HDU - 4336:Card Collector(min-max容斥求期望)

    In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...

  3. HDU 4135 Co-prime(容斥:二进制解法)题解

    题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...

  4. ARC096 E Everything on It [容斥,斯特林数]

    Atcoder 一个900分的题耗了我这么久--而且官方题解还那么短--必须纪念一下-- 思路 发现每种元素必须出现两次以上的限制极为恶心,所以容斥,枚举出现0/1次的元素个数分别有几个.设出现1次的 ...

  5. 【BZOJ】4767: 两双手【组合数学】【容斥】【DP】

    4767: 两双手 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1057  Solved: 318[Submit][Status][Discuss] ...

  6. 【51nod1253】Kundu and Tree(容斥+并查集)

    点此看题面 大致题意: 给你一棵树,每条边为黑色或红色, 求有多少个三元组\((x,y,z)\),使得路径\((x,y),(x,z),(y,z)\)上都存在至少一条红色边. 容斥 我们可以借助容斥思想 ...

  7. HDU 4135 Co-prime(容斥+数论)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. 【XSY3156】简单计数II 容斥 DP

    题目大意 定义一个序列的权值为:把所有相邻的相同的数合并为一个集合后,所有集合的大小的乘积. 特别的,第一个数和最后一个数是相邻的. 现在你有 \(n\) 种数,第 \(i\) 种有 \(c_i\) ...

  9. bzoj4665 小w的喜糖(dp+容斥)

    4665: 小w的喜糖 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 222  Solved: 130[Submit][Status][Discuss ...

随机推荐

  1. webpack笔记一

    gulp和grunt都以task来运行不同的命令,而看webpack相关文档都是webpack都是module.下面是一个简单的demo 一.目录解构 二.webpack.config.js文件 co ...

  2. IIS/ASP.NET访问共享文件夹的可用方式

    [截止2014-10-14] 网上搜索了很多篇文章,所提及的总共有两种方式: 1.Asp.Net模拟登陆: 例如: 实战ASP.NET访问共享文件夹(含详细操作步骤) 实现一个2008serve的II ...

  3. gh-ost安装

    下载 : https://github.com/github/gh-ost/releases/tag/v1.0.28 先安装Go语言: sudo yum install golang 将gh-ost文 ...

  4. ssm返回jsonp数据格式

    为了便于客户端使用数据,逐渐形成了一种非正式传输协议,人们把它称作JSONP,该协议的一个要点就是允许用户传递一个callback参数给服务端,然后服务端返回数据时会将这个callback参数作为函数 ...

  5. (C#) SQLite数据库连接字符串

    最常用的:Data Source=filename;Version=3; 自增主键: Create  test1( [id] integer PRIMARY KEY AUTOINCREMENT ,[n ...

  6. windows下编译和安装boost库

    boost是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库. 获取方式 boost提供源码形式的安装包,可以从boost官方网站下载,目前最新版本是1.59.0. 本机上正好有boos ...

  7. 深入浅出TCP之listen

    原文:http://blog.chinaunix.net/uid-29075379-id-3858844.html int listen(int fd, int backlog); 有几个概念需要在开 ...

  8. QPixmap 显示大小

    size picSize(600,400); //将pixmap缩放成picSize大小然后保存在scaledPixmap中 按比例缩放: QPixmap scaledPixmap = pixmap. ...

  9. poj2932 Coneology

    地址:http://poj.org/problem?id=2932 题目: Coneology Time Limit: 5000MS   Memory Limit: 65536K Total Subm ...

  10. Linux root用户下不能打开Google-chrome的解决办法

    在root下打开chrome会出现no sandbox的错误 解决方案: 1.找到google-chrome文件 在目录/opt/google/chrome 下 2.使用gedit打开该文件 最后一行 ...