题目链接:点击查看

题目大意:给出一个 n∗mn*mn∗m 的矩阵,其中有 kkk 个坏点,每次只能向右走或向下走,问从点 (1,1)(1,1)(1,1) 到点 (n,m)(n,m)(n,m) 共有多少种不同的路线

题目分析:刷知乎看到的一道题,心血来潮就想写题了

数据范围 n,mn,mn,m 都是 1e51e51e5 级别的,而 kkk 却只有 200020002000,所以从坏点入手,考虑 dpdpdp 和组合数学

首先对于两个点 (x1,y1)(x1,y1)(x1,y1) 和 (x2,y2)(x2,y2)(x2,y2) 来说,如果没有坏点的限制,那么这两个点之间的路线有 C(x2−x1)+(y2−y1)x2−x1C_{(x2-x1)+(y2-y1)}^{x2-x1}C(x2−x1)+(y2−y1)x2−x1​ 条,下面可以分两步进行思考,对于每个坏点 (x,y)(x,y)(x,y) 来说:

  1. 加上点 (1,1)(1,1)(1,1) 到点 (x,y)(x,y)(x,y) 之间方案数
  2. 对于每个满足 xx<=xxx<=xxx<=x 且 yy<=yyy<=yyy<=y 的坏点 (xx,yy)(xx,yy)(xx,yy) 来说,点 (1,1)(1,1)(1,1) 经过点 (xx,yy)(xx,yy)(xx,yy) 然后到达点 (x,y)(x,y)(x,y) 的这些路线显然是不合法的,减去即可

综上所述,将所有坏点排序之后的转移方程就是(设 f(x1,y1)(x2,y2)f_{(x1,y1)}^{(x2,y2)}f(x1,y1)(x2,y2)​ 为坏点 (x1,y1)(x1,y1)(x1,y1) 到坏点 (x2,y2)(x2,y2)(x2,y2) 的方案数,dpidp_idpi​ 为点 (1,1)(1,1)(1,1) 到第 iii 个坏点的方案数):
dpi=f(1,1)(pi.x,pi.y)−[pj.x<=pi.x&&pj.y<=pi.y]∗dpj∗f(pi.x,pi.y)(pj.x,pj.y)dp_i=f_{(1,1)}^{(p_i.x,p_i.y)} \\ -[p_j.x<=p_i.x\ \&\&\ p_j.y<=p_i.y]*dp_j*f_{(p_i.x,p_i.y)}^{(p_j.x,p_j.y)} dpi​=f(1,1)(pi​.x,pi​.y)​−[pj​.x<=pi​.x && pj​.y<=pi​.y]∗dpj​∗f(pi​.x,pi​.y)(pj​.x,pj​.y)​
将点 (n,m)(n,m)(n,m) 设为第 k+1k+1k+1 个坏点,那么答案自然就是 dpk+1dp_{k+1}dpk+1​ 了

代码:

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
const int mod=1e9+7;
struct Point {int x,y;void input() {read(x),read(y);}bool operator<(const Point& t)const {if(x!=t.x) {return x<t.x;}return y<t.y;}
}p[N];
LL dp[N],fac[N],inv[N];
LL q_pow(LL a,LL b) {LL ans=1;while(b) {if(b&1) {ans=ans*a%mod;}a=a*a%mod;b>>=1;}return ans;
}
LL C(int n,int m) {if(n<m) {return 0;}return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
void init() {fac[0]=1;for(int i=1;i<N;i++) {fac[i]=fac[i-1]*i%mod;}inv[N-1]=q_pow(fac[N-1],mod-2);for(int i=N-2;i>=0;i--) {inv[i]=inv[i+1]*(i+1)%mod;}
}
int main()
{#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);init();int n,m,k;read(n),read(m),read(k);for(int i=1;i<=k;i++) {p[i].input();}p[++k]={n,m};sort(p+1,p+1+k);for(int i=1;i<=k;i++) {dp[i]=C((p[i].x-1)+(p[i].y-1),(p[i].x-1));for(int j=1;j<i;j++) {if(p[i].x>=p[j].x&&p[i].y>=p[j].y) {dp[i]=(dp[i]-dp[j]*C((p[i].x-p[j].x)+(p[i].y-p[j].y),p[i].x-p[j].x)%mod+mod)%mod;}}}write(dp[k]);return 0;
}

CodeForces - 560E Gerald and Giant Chess(组合数学+dp)相关推荐

  1. 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 ...

  2. 数论五之容斥——硬币购物,Gerald and Giant Chess,幸运数字,Sky Full of Stars,已经没有什么好害怕的了

    容斥的神 [HAOI2008]硬币购物 problem solution code CF559C Gerald and Giant Chess problem solution code [SCOI2 ...

  3. [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 ...

  4. Codeforces Gym 100338H High Speed Trains 组合数学+dp+高精度

    原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...

  5. codeforces 869C The Intriguing Obsession【组合数学+dp+第二类斯特林公式】

    C. The Intriguing Obsession time limit per test 1 second memory limit per test 256 megabytes input s ...

  6. CodeForces 215 E.Periodical Numbers(组合数学+dp)

    Description 给出一个区间[L,R][L,R],问这个区间中有多少数字的二进制表示是有循环节的 Input 两个正整数L,RL,R(1≤L≤R≤1018)(1\le L\le R\le 10 ...

  7. cf559C. Gerald and Giant Chess(容斥原理)

    题意 $h \times w$的网格,有$n$个障碍点, 每次可以向右或向下移动 求从$(1, 1)$到$(h, w)$不经过障碍点的方案数 Sol 容斥原理 从$(1, 1)$到$(h, w)$不经 ...

  8. Codeforces 1546 D. AquaMoon and Chess —— 组合数学,一点点想法

    This way 题意: 给你一个01串s,你每次可以做一个操作如果s[i]=1: 1.如果i+2<=n且s[i+2]=0且s[i+1]=1,那么可以将s[i]的1移到s[i+2] 2.如果i- ...

  9. codeforces #274 C. Riding in a Lift dp+前缀和优化

    codeforces #274  C. Riding in a Lift   dp+前缀和优化 Imagine that you are in a building that has exactly  ...

最新文章

  1. 暑期集训2:ACM基础算法 例2:POJ-2456
  2. mysql8.0_grant改变-You are not allowed to create a user with GRANT
  3. Spring中的AOP在Advice方法中获取目标方法的参
  4. 利用数据集在水晶报表中显示图像
  5. 洛谷P3607:Subsequence Reversal P(区间dp)
  6. 用python画渐变的圆_使用numpy绘制圆形渐变
  7. NCrawler爬取中文网页时乱码问题的解决方法
  8. html快照抓取,请教前端实现获取dom元素快照的方法
  9. java基础方法笔记
  10. tensorflow 提示没有models库
  11. 数据-第19课-递归的应用实战一
  12. Unity自定义UI组件(九) 颜色拾取器(下)
  13. 一个SQL SERVER查询分析器非常好用的工具
  14. 清理xcode模拟器_mac太卡,清除xcode和模拟器缓存
  15. 电脑在使用b站的时候插入耳机,耳机没有声音,只能外放,其他软件可以正常使用。
  16. 台式计算机亮度设置,怎么调台式电脑亮度_怎么调台式电脑的亮度
  17. 电邮地址_电子邮件如何运作?
  18. 17秋《计算机应用基础》,南开17秋学期《计算机应用基础》在线作业.docx
  19. Springboot毕设项目医疗云胶片管理系统nem7xjava+VUE+Mybatis+Maven+Mysql+sprnig)
  20. 《楚汉传奇》对韩信的一点看法

热门文章

  1. android 开发 分辨率,Android手机应用开发为适应不同分辨率你应该知道的
  2. c++如何将两个if函数合并_设计一个 add 函数
  3. jdbctemplate mysql 分页查询 返回list对象_spring jdbctemplate调用存储过程,返回list对象...
  4. container到image
  5. 序列化的高阶认识-Transient 关键字
  6. synchronized的基本语法
  7. 工程搭建:搭建子工程之搭建环境构造返回实体类
  8. 新版本springboot整合@transactional注解
  9. jvm_堆栈永久区详细讲解
  10. adb命令 android 串口_ADB使用linux命令查看Android的使用情况