cf559C. Gerald and Giant Chess(容斥原理)
题意
$h \times w$的网格,有$n$个障碍点,
每次可以向右或向下移动
求从$(1, 1)$到$(h, w)$不经过障碍点的方案数
Sol
容斥原理
从$(1, 1)$到$(h, w)$不经过障碍点的方案数为$C(h + w, h)$
设$f[i]$表示到达第$i$个黑格子的合法路径的方案数
首先对所有点按$x$排序,这样就能保证每次从他的左上方转移而来
然后根据公式算一下就好了
// luogu-judger-enable-o2 #include<iostream> #include<algorithm> #include<cstdio> #include<stack> #include<vector> #include<cstring> #define Pair pair<int, int> #define MP(x, y) make_pair(x, y) #define fi first #define se second #define int long long //#define int long long using namespace std; const int MAXN = 3 * 1e6, mod = 1e9 + 7; inline int read() {char c = getchar(); int x = 0, f = 1;while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();}while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();return x * f; } Pair P[MAXN]; int h, w, N; int fac[MAXN], ifac[MAXN], f[MAXN]; int fastpow(int a, int p) {int base = 1;while(p) {if(p & 1) base = (base * a) % mod;a = (a * a) % mod; p >>= 1;}return base % mod; } int C(int N, int M) {return (fac[N] * ifac[M] % mod * ifac[N - M]) % mod; } main() {h = read(), w = read(); N = read() + 1; fac[0] = 1; for(int i = 1; i <= h + w; i++) fac[i] = i * fac[i - 1] % mod;ifac[h + w] = fastpow(fac[h + w], mod - 2); for(int i = h + w; i >= 1; i--) ifac[i - 1] = i * ifac[i] % mod;for(int i = 1; i <= N - 1; i++) {int x = read(), y = read();P[i] = MP(x, y);}P[N] = MP(h, w);sort(P + 1, P + N + 1);for(int i = 1; i <= N; i++) {f[i] = C(P[i].fi + P[i].se - 2, P[i].fi - 1);for(int j = i - 1; j >= 1; j--) {if(P[j].se <= P[i].se) {int x = P[i].fi - P[j].fi + 1, y = P[i].se - P[j].se + 1;(f[i] -= f[j] * C(x + y - 2, x - 1) % mod + mod) %= mod;}}}printf("%I64d", (f[N] + mod) % mod);return 0; } /* 2 3 2 2 1 2 2 */
cf559C. Gerald and Giant Chess(容斥原理)相关推荐
- [CF559C] Gerald and Giant Chess
Gerald and Giant Chess 题意: 给你一个 h × w h\times w h×w的网格和 n n n个黑点,问你从 ( 1 , 1 ) (1, 1) (1,1)到 ( h , w ...
- 数论五之容斥——硬币购物,Gerald and Giant Chess,幸运数字,Sky Full of Stars,已经没有什么好害怕的了
容斥的神 [HAOI2008]硬币购物 problem solution code CF559C Gerald and Giant Chess problem solution code [SCOI2 ...
- Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess DP
C. Gerald and Giant Chess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- CodeForces - 560E Gerald and Giant Chess(组合数学+dp)
题目链接:点击查看 题目大意:给出一个 n∗mn*mn∗m 的矩阵,其中有 kkk 个坏点,每次只能向右走或向下走,问从点 (1,1)(1,1)(1,1) 到点 (n,m)(n,m)(n,m) 共有多 ...
- CF559C-Gerald and Giant Chess【计数类dp】
正题 上不了Codeforces,就用洛谷了 评测记录:https://www.luogu.org/recordnew/lists?uid=52918&pid=CF559C 题目大意 H∗WH ...
- CF刷题(03)——难度2100~2400
这个博客记录2100到2400共17个题 2100 1.B. Maximum Value 题意:You are given a sequence a consisting of nnn integer ...
- opencv国际象棋_国际象棋是的
opencv国际象棋 By Kellen Browning 凯伦·布朗宁 On a recent afternoon, thousands of noncombatants watched from ...
- HDU 4135 Co-prime(容斥原理)
Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...
- BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 3028 Solved: 1460 [Submit][S ...
最新文章
- 一款优秀的JavaScript框架—AngularJS
- 《第一行代码》学习笔记24-持久化技术(3)
- 主流云服务器购买平台
- 使用CDN引用jQuery
- argparse:shell向Python中传参数
- GridView CSS的样式表
- 年薪40W,如何高效准备大厂AI算法岗面试?
- 流量中转的服务器,你的服务器能承受多大流量
- jpanel把原本内容覆盖掉_暖冬遇上倒春寒,花被大雪覆盖,小心一夜回到解放前...
- jvm压缩指针原理以及32g内存压缩指针失效详解
- 试题7 算法训练 P0505(最右边的那个非0的数字)
- MEMS:万物智联技术关键
- 【Python】使用torrentParser1.03对单文件torrent的分析结果
- Matlab中的plotyy用法总结
- ubuntu安装smplayer
- 【Android】【TP】TP开发常见问题分析
- 解决一个远程主机强迫关闭连接的bug
- MAC笔记本破解ZIP压缩的文件密码
- dis反汇编文件的分析理解
- IDC发布中国企业网盘报告:市场保持高速增长 联想企业网盘蝉联第一
热门文章
- 六轴机器人 宝元系统_庆云大国重器上线六轴智能焊接机器人,锻造高质量发展硬核...
- 全面覆盖CV任务!这个国产“书生”只学10%内容,性能就超越同行
- 为了在元宇宙里摸到东西,扎克伯格整出了一款新电子皮肤,成本6美元
- 用上强化学习和博弈论,EA开发的测试AI成精了
- 2021年第一天,腾讯给青年科学家的300万大红包又来了
- 如何0代码、快速定制企业级NLP模型?百度工程师详解技术选型与模型调优策略...
- redis单机版安装
- 2.pandas数据清洗
- 【290】Python 函数
- 直播卡顿原因详解及优化