FJNUOJ Yehan’s hole(容斥求路径数 + 逆元)题解
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(容斥求路径数 + 逆元)题解的更多相关文章
- HDU-2204-Eddy's爱好-容斥求n以内有多少个数形如M^K
HDU-2204-Eddy's爱好-容斥求n以内有多少个数形如M^K [Problem Description] 略 [Solution] 对于一个指数\(k\),找到一个最大的\(m\)使得\(m^ ...
- HDU - 4336:Card Collector(min-max容斥求期望)
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...
- HDU 4135 Co-prime(容斥:二进制解法)题解
题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...
- ARC096 E Everything on It [容斥,斯特林数]
Atcoder 一个900分的题耗了我这么久--而且官方题解还那么短--必须纪念一下-- 思路 发现每种元素必须出现两次以上的限制极为恶心,所以容斥,枚举出现0/1次的元素个数分别有几个.设出现1次的 ...
- 【BZOJ】4767: 两双手【组合数学】【容斥】【DP】
4767: 两双手 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1057 Solved: 318[Submit][Status][Discuss] ...
- 【51nod1253】Kundu and Tree(容斥+并查集)
点此看题面 大致题意: 给你一棵树,每条边为黑色或红色, 求有多少个三元组\((x,y,z)\),使得路径\((x,y),(x,z),(y,z)\)上都存在至少一条红色边. 容斥 我们可以借助容斥思想 ...
- HDU 4135 Co-prime(容斥+数论)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 【XSY3156】简单计数II 容斥 DP
题目大意 定义一个序列的权值为:把所有相邻的相同的数合并为一个集合后,所有集合的大小的乘积. 特别的,第一个数和最后一个数是相邻的. 现在你有 \(n\) 种数,第 \(i\) 种有 \(c_i\) ...
- bzoj4665 小w的喜糖(dp+容斥)
4665: 小w的喜糖 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 222 Solved: 130[Submit][Status][Discuss ...
随机推荐
- Cable master---poj1064(二分|卡精度)
题目链接:http://poj.org/problem?id=1064 题意:有n条绳子,长度为Li,现在从这n条绳子中切割出K条相等的绳子,问切割出来的这k条绳子的最大长度为多少: 二分判断即可: ...
- Charles maplocal 时中文显示乱码问题
用Charles对request进行Map Local后,app上看返回的中文是乱码? 是Map Local的文件编码有问题?是Charles设置有问题?是电脑环境有问题?哈哈,都不是 你是Andro ...
- struts2 OGNL(Object-Graph Navigation Language) 井号,星号,百分号
1.“#”主要有三种用途: 访问OGNL上下文和Action上下文,#相当于ActionContext.getContext():可以访问这几个ActionContext中的属性. parameter ...
- hibernate注解(一)JoinColumn
@Entity @Table(name="t_group") public class Group { private int id; private String name; p ...
- soapUI-JDBC Request
1.1.1 JDBC Requet 1.1.1.1 概述 – JDBC Request Option Description JDBC Request TestStep Toolbar 对JDB ...
- nodejs+express的(前端跨域请求)
1.后端代码 var dp = 456; var back = 'callback(\{\dp\ : \ ' + dp + '\ }\)'; res.send(back); 2.前端代码 <sc ...
- 机器学习 python库 介绍
开源机器学习库介绍 MLlib in Apache Spark:Spark下的分布式机器学习库.官网 scikit-learn:基于SciPy的机器学习模块.官网 LibRec:一个专注于推荐算法的j ...
- php分层
表示层 UI 主要表示WEB方式,也可以表示成WINFORM方式.如果逻辑层相当强大和完善,无论表现层如何定义和更改,逻辑层都能完善地提供服务. 业务逻辑层 BLL ...
- Azkaban 简介
本文简单介绍一下Azkaban及其特点.azkaban是一个开源的任务调度系统,用于负责任务的调度运行(如数据仓库调度),用以替代linux中的crontab. 一.Azkaban是什么? 1.1 A ...
- Linux 使用系统ISO制作yum源
关于linux安装问题,大多数情况下 系统开发完成之后,需要部署到生产机器上,客户提供的机器预装好了操作系统,但是都是内网环境 与外网都是物理隔绝的,那么 在搭建生产环境时需要安装相关软件时,如果自己 ...