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 ...
随机推荐
- T-SQL中的十大注意事项
转载自:http://www.cnblogs.com/CareySon/archive/2012/10/11/2719598.html 1.在生产环境中不要出现Select * 这一点我想大家已经是比 ...
- mysql 权限管理 针对表的字段 级别 授权 columns_priv表
针对Mike账号 db1库下面的t1表的 id,name字段授予select权限,age字段授予update权限 授权格式 select(要授权的字段,要授权的字段) 用户括号 括起来 .updat ...
- Netty In Action中文版 - 第三章:Netty核心概念
在这一章我们将讨论Netty的10个核心类.清楚了解他们的结构对使用Netty非常实用.可能有一些不会再工作中用到.可是也有一些非经常常使用也非常核心,你会遇到. Bootstrap ...
- 【Swift初见】SourceKitService Terminated
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/weasleyqi/article/details/36162085 心血来潮想试试最新的Xcode6 ...
- js将form表单序列化[json字符串、数组、对象]
1.序列化为字符串 $("#Form").serialize();//name=zhangsan&sex=1&age=20 2.序列化为数组 var formD ...
- 编译错误 ----- /usr/bin/ld: cannot find -lc
yum install glibc-static glib-static是Gcc链接时使用到的库.
- Django之分页功能
Django提供了一个新的类来帮助你管理分页数据,这个类存放在django/core/paginator.py.它可以接收列表.元组或其它可迭代的对象. 基本语法 class Paginator(ob ...
- ios 工作日志
1.设计模式 1.1 想用一个controllerK控制多个页面的切换 但是每一个页面必须要引用这个controller,这样才能控制进度, 所以必须是弱引用.controller必须被某一个实例强引 ...
- Vue项目图片剪切上传——vue-cropper的使用
最近自己在研究vue,然后做了一个小型的后台管理系统用来练手,开发过程中,想到了剪切图片上传用户头像的需求.上网百度了一番,发现好多用的都是vue-cropper.我也就用了,个人感觉还是挺好用的.现 ...
- 使用Linux工作之Fedora KDE
小明拿着在Windows下不断蓝屏的T440和公司建议不使用云笔记的规定,心下想着,是时候回归linux了... 一.系统的获取与启动盘的制作 fedora20 KDE版 liveusb-creato ...