link

试题分析

我们发现普通$dp$时间复杂度为$O(h \times w)$的,会$T$的很惨。而这个又无法通过优化,所以呢就要改变$dp$策略。

观察到$n\leq 2000$,所以我们需要设计出一个关于不能走的$dp$。

part1 排列组合应用

$C_i^j$的意思大家都知道把,但是这道题又与排列组合有什么关系呢。易证$C_{n+m}^n$的结果正好是从$(0,0)$走到$(n,m)$的方案数,通过插板法可证。

所以若我们从$(1,1)$出发,到终点$(n,m)$的方案数为$C_{n+m-2}^{n-1}$。

part2 dp

所以说$dp$式子就很好写了,$f(i)$表示只经过$i$号黑点的方案数,其余黑点均不参加。同时将最后所要求的$(h,w)$当作一个黑点。

则:$f(i)={C_{x_i+y_i-2}^{x_i-1}}-f(j)\times C_{x_i-x_j+y_i-y_j}^{x_i-x_j}  (j点在i点的左上角)。$

然后再用逆元求一下即可。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define int long long
#define mod 1000000007
using namespace std;
inline int read(){int f=1,ans=0;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}return f*ans;
}
const int N=200011;
int inv[N],fac[N];
int ksm(int a,int b){int ans=1;while(b){if(b&1) ans*=a,ans%=mod;a*=a,a%=mod;b>>=1;}return ans%mod;
}
int n,m,k;
struct node{int x,y;
}a[N];
int C(int m,int n){if(n==0) return 1;return (fac[m]*((inv[n]*inv[m-n])%mod))%mod;}
bool cmp(node x1,node x2){if(x1.x==x2.x) return x1.y<x2.y;return x1.x<x2.x;
}
int f[N];
signed main(){inv[0]=1,fac[0]=1;for(int i=1;i<=200001;i++){fac[i]=(fac[i-1]*i)%mod;inv[i]=ksm(fac[i],mod-2);}n=read(),m=read(),k=read();for(int i=1;i<=k;i++) a[i].x=read(),a[i].y=read();sort(a+1,a+k+1,cmp);a[++k].x=n,a[k].y=m;for(int i=1;i<=k;i++){f[i]=C(a[i].x+a[i].y-2,a[i].x-1)%mod;for(int j=1;j<i;j++){if(a[j].x>a[i].x||a[j].y>a[i].y) continue;f[i]-=f[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x);f[i]=((f[i]%mod)+mod)%mod;}}printf("%lld",(f[k]%mod+2*mod)%mod);
}

View Code

转载于:https://www.cnblogs.com/si-rui-yang/p/10145555.html

Gerald and Giant Chess相关推荐

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

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

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

  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. cf559C. Gerald and Giant Chess(容斥原理)

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

  5. CodeForces - 560E Gerald and Giant Chess(组合数学+dp)

    题目链接:点击查看 题目大意:给出一个 n∗mn*mn∗m 的矩阵,其中有 kkk 个坏点,每次只能向右走或向下走,问从点 (1,1)(1,1)(1,1) 到点 (n,m)(n,m)(n,m) 共有多 ...

  6. CF559C-Gerald and Giant Chess【计数类dp】

    正题 上不了Codeforces,就用洛谷了 评测记录:https://www.luogu.org/recordnew/lists?uid=52918&pid=CF559C 题目大意 H∗WH ...

  7. CF刷题(03)——难度2100~2400

    这个博客记录2100到2400共17个题 2100 1.B. Maximum Value 题意:You are given a sequence a consisting of nnn integer ...

  8. opencv国际象棋_国际象棋是的

    opencv国际象棋 By Kellen Browning 凯伦·布朗宁 On a recent afternoon, thousands of noncombatants watched from ...

  9. HDU 6114 Chess 【组合数】(2017百度之星程序设计大赛 - 初赛(B))

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  10. hdu-5794 A Simple Chess(容斥+lucas+dp)

    题目链接: A Simple Chess Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

最新文章

  1. Linux(centOS)手动安装Apache+MySQL+PHP+Memcached+Nginx原创无错版
  2. pandas使用dropna函数删除dataframe中列非缺失值的个数小于某一比例阈值的数据列
  3. 四款 5G 版 iPhone 12 齐发,支持北斗系统,你准备好了吗?
  4. linux shell 算术运算{expr、bc、dc、(( ))和[ ]}
  5. python是用什么语言开发的-python是什么语言?哪些人适合学习Python?
  6. Python日志详解【两篇就够了系列】--第二篇loguru
  7. 算法入门开灯问题,新做法
  8. php mysql 安装错误_Apache+php配置 Mysql安装出错解决办法
  9. P2490-[SDOI2011]黑白棋【博弈论,dp】
  10. jsp论坛网站模版_网站关键词优化怎么做
  11. 安装ubuntu18.04虚拟机太慢
  12. 关于CAS服务器磁盘占用的问题,锁定目录惹的祸
  13. org.apache.ibatis.builder.BuilderException: An invalid property ‘jdbcType ‘ was found in mapping
  14. MD5 加密算法详细介绍
  15. ubuntu安装github 3D渲染库dirt
  16. YOLO V5 实现课堂行为检测
  17. dedecms 安装后 管理后台ie假死 无响应的解决方法
  18. 第一章 Java性能调优概述
  19. 国际汇款之账户体系与记账处理
  20. MSDC 4.3 接口规范(17)

热门文章

  1. 拓端tecdat|R语言中回归模型预测的不同类型置信区间应用比较分析
  2. 拓端tecdat|R 语言绘制功能富集泡泡图
  3. python 连接Oracle数据库,cx_Oracle
  4. j2ee和mysql怎么连接_Eclipse下配置j2ee开发环境及与MySQL数据库的连接
  5. 用caffe训练测试自己的图片
  6. SQLServer数据库增、删、改、查简单操作示例
  7. datagrid设置单元格边框_更改 DataGridView 控件中的边框和网格线样式 - Windows Forms .NET Framework | Microsoft Docs...
  8. Pareto Optimality 帕累托最优 是什么
  9. DAG(有向无环图)有向树 转换为树
  10. 关于序列化的几个注意点