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 ...
随机推荐
- CentOS 6 网络设置
系统配置: 系统硬件:vmware workstation 系统版本:Centos-6.6-x86_64 路由器网关:192.168.1.1 linux系统网络设置须知: 1.主机所有网卡信息配置文件 ...
- list的方法、操作
序号 分类 关键字 / 函数 / 方法 说明 1 增加 列表.insert(索引, 数据) 在指定位置插入数据 列表.append(数据) 在末尾追加数据 列表.extend(列表2) ...
- 【剑指offer】替换空格
一.题目: 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 二.思路: Python代码,先 ...
- Python3之socket编程
一.socket的定义 Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后 ...
- (转)Elasticsearch查询规则------match和term
es种有两种查询模式,一种是像传递URL参数一样去传递查询语句,被称为简单搜索或查询字符串(query string)搜索,比如 GET /megacorp/employee/_search //查询 ...
- Andrew Ng-ML-第十五章-降维
1.数据压缩 数据压缩不仅能够减小存储空间,并且能够加速学习算法.那么什么是数据压缩呢?下面给出了一个简单的例子: 图1.数据压缩的概念 举了两个例子,一个是横轴x1是厘米,纵轴特征x2是英尺,这明显 ...
- [LeetCode] 438. Find All Anagrams in a String_Easy
438. Find All Anagrams in a String DescriptionHintsSubmissionsDiscussSolution Pick One Given a str ...
- Windows多线程基础
进程与线程基础 程序: 计算机指令的集合,以文件的形式存储在磁盘上 进程: 正在运行是程序实例,以是一个程序在其自身的地址空间的一次执行活动.进程有一个进程管理的内核对象和地址空间组成. 线程: 程序 ...
- Geometry
uva1473 这题说的是 在空间中给了n个点 然后用体积最小的圆锥将这些点包含在内可以在表面上, 将这些点 映射到xoz平面上然后,然后枚举每个上凸包的边和每个点的极值进行判断求得最小的体积 我们会 ...
- 10588 - Queuing at the doctors
这题wa 了 八次 你说 巨弱 orz 大神 总结一下 没有将所有的情况考虑清楚 ,当有的时候一个人已经全部看完的时候 别的人还没开始 但是我就把他给结束了 #include <iost ...