2018.11.07 codeforces559C. Gerald and Giant Chess(dp+组合数学)
传送门
令f[i]f[i]f[i]表示对于第iii个棋子,从(1,1)(1,1)(1,1)出发到它不经过其它棋子的方案数。
于是我们假设(h,w)(h,w)(h,w)有一个棋子,求出它的fff值就可以了。
然后考虑容斥转移fff数组。
根据定义,我们求出从(1,1)(1,1)(1,1)出发到它的总方案数,再减去经过了其它棋子的方案数。
然后再考虑如何才会补充不漏。
发现从之前每一个fff转移过来就行了。
fi=(xi+yi−2xi−1)−∑fj∗(xi−yi+xj−yjxi−xj)f_i=\binom{x_i+y_i-2}{x_i-1}-\sum f_j*\binom{x_i-y_i+x_j-y_j}{x_i-x_j}fi=(xi−1xi+yi−2)−∑fj∗(xi−xjxi−yi+xj−yj)
代码:
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
typedef long long ll;
const int N=2e3+5,M=1e5+5,mod=1e9+7;
int h,w,n,f[N],fac[M+M],ifac[M+M];
struct Node{
int x,y;
friend inline bool operator<(const Node&a,const Node&b){return a.x==b.x?a.y<b.y:a.x<b.x;}
}p[N];
inline int C(const int&n,const int&m){return (ll)fac[n]*ifac[m]%mod*ifac[n-m]%mod;}
int main(){
h=read(),w=read(),n=read(),fac[0]=fac[1]=ifac[0]=ifac[1]=1;
for(int i=2;i<=h+w;++i)fac[i]=(ll)fac[i-1]*i%mod;
for(int i=2;i<=h+w;++i)ifac[i]=(ll)ifac[mod%i]*(mod-mod/i)%mod;
for(int i=2;i<=h+w;++i)ifac[i]=(ll)ifac[i]*ifac[i-1]%mod;
for(int i=1;i<=n;++i)p[i].x=read(),p[i].y=read();
p[++n]=(Node){h,w},sort(p+1,p+n+1);
for(int i=1;i<=n;++i){
f[i]=C(p[i].x+p[i].y-2,p[i].x-1);
for(int j=1;j<i;++j){
if(p[j].y>p[i].y)continue;
f[i]=f[i]-(ll)f[j]*C(p[i].x+p[i].y-p[j].x-p[j].y,p[i].x-p[j].x)%mod;
if(f[i]<0)f[i]+=mod;
}
}
cout<<f[n];
return 0;
}
2018.11.07 codeforces559C. Gerald and Giant Chess(dp+组合数学)的更多相关文章
- 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 ...
- Codeforces559C Gerald and Giant Chess
一道计数类\(DP\) 原题链接 我们可以先计算从左上角到右下角总的路径,再减去经过黑色方格的路径即是答案. 总路径数可以用组合数直接计算:\(C_{H+W-2}^{H-1}\) 因为从左上角到右下角 ...
- 2018.11.07 bzoj2751: [HAOI2012]容易题(easy)(组合数学)
传送门 组合数学一眼题. 感觉一直做这种题智商会降低. 利用组合数学的分步计数原理. 只用关心每个数不被限制的取值的总和然后乘起来就可以了. 对于大部分数都不会被限制,总和都是n(n+1)2\frac ...
- Codeforces 559C Gerald and Giant Chess【组合数学】【DP】
LINK 题目大意 有一个wxh的网格,上面有n个黑点,问你从(1,1)走到(w,h)不经过任何黑点的方案数 思路 考虑容斥 先把所有黑点按照x值进行排序方便计算 \(dp_{i}\)表示从起点走到第 ...
- E. Gerald and Giant Chess
E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...
- 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)
[题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...
- dp - Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess
Gerald and Giant Chess Problem's Link: http://codeforces.com/contest/559/problem/C Mean: 一个n*m的网格,让你 ...
- CodeForces 559C Gerald and Giant Chess
C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Gerald and Giant Chess
Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- phpStudy安装教程
1.在phpStudy官网下载安装包(http://phpstudy.php.cn/)2.解压安装后,若提示没有“VC9.VC11.VC14运行库,注意是X86 32位”,则在phpStudy下载对应 ...
- MySQL的四种不同查询的分析
1.前置条件: 本次是基于小数据量,且数据块在一个页中的最理想情况进行分析,可能无具体的实际意义,但是可以借鉴到各种复杂条件下,因为原理是相同的,知小见大,见微知著! 打开语句分析并确认是否已经打开 ...
- 项目总结07:JS图片的上传预览和表单提交(FileReader()方法)
JS图片的上传预览和表单提交(FileReader()方法) 一开始没有搞明白下面这块代码的,今天有时间简单整理下 核心点:FileReader()方法 以下是代码(以JSP文件为例) <!DO ...
- tcp中的keepalive(转)
理解Keepalive(1) 大家都听过keepalive,但是其实对于keepalive这个词还是很晦涩的,至少我一直都只知道一个大概,直到之前排查线上一些问题,发现keepalive还是有很多玄机 ...
- android屏蔽BACK键、HOME键和多任务键
HOME: @Overridepublic void onAttachedToWindow() { System.out.println("Page01 -->onAttachedTo ...
- wheelView实现滚动选择 三方开源的封装控件 spannableString autofitTextView、PinnedSectionListView SwipeListView等等
wheelView多用于popupwindow用来滚动选择条目 github上的开源三方控件 spannableString autofitTextView.PinnedSectionLi ...
- listView 解决焦点冲突问题 item项和子控件之间的冲突
listView 在item布局的顶级布局中设置 android:descendantFocusability="blocksDescendants"可以阻止子控件获取焦点 这样使 ...
- 纯css实现div中未知尺寸图片的垂直居中
1.淘宝的方法 在曾经的"淘宝UED招聘"中有这样一道题目: “使用纯CSS实现未知尺寸的图片(但高宽都小于200px)在200px的正方形容器中水平和垂直居中.” 当然出题并不是 ...
- 针对appium的webdriver执行swipe无效的解决办法
self.driver.swipe(x1,y1,x2,y1,t) 当时代码里有如上这么一句,当时源码是这么说的: # convenience method added to Appium (NOT S ...
- 进程同步(multiprocess.Lock、multiprocess.Semaphore、multiprocess.Event) day38
进程同步(multiprocess.Lock.multiprocess.Semaphore.multiprocess.Event) 锁 —— multiprocess.Lock 通过刚刚的学习,我们千 ...