传送门

令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​−xj​xi​−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+组合数学)的更多相关文章

  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. Codeforces559C Gerald and Giant Chess

    一道计数类\(DP\) 原题链接 我们可以先计算从左上角到右下角总的路径,再减去经过黑色方格的路径即是答案. 总路径数可以用组合数直接计算:\(C_{H+W-2}^{H-1}\) 因为从左上角到右下角 ...

  3. 2018.11.07 bzoj2751: [HAOI2012]容易题(easy)(组合数学)

    传送门 组合数学一眼题. 感觉一直做这种题智商会降低. 利用组合数学的分步计数原理. 只用关心每个数不被限制的取值的总和然后乘起来就可以了. 对于大部分数都不会被限制,总和都是n(n+1)2\frac ...

  4. Codeforces 559C Gerald and Giant Chess【组合数学】【DP】

    LINK 题目大意 有一个wxh的网格,上面有n个黑点,问你从(1,1)走到(w,h)不经过任何黑点的方案数 思路 考虑容斥 先把所有黑点按照x值进行排序方便计算 \(dp_{i}\)表示从起点走到第 ...

  5. E. Gerald and Giant Chess

    E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...

  6. 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)

    [题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...

  7. 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的网格,让你 ...

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

  9. Gerald and Giant Chess

    Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

随机推荐

  1. Intent Activity跳转 传递数据 Bundle

    1.普通跳转: Intent intent=new Intent(); intent.setClass(MainActivity.this,NewActivity.class); //新建一个Inte ...

  2. C++ map与unordered_map

    map与unordered_map对比 map unordered_map 红黑树(非严格二叉平衡搜索树)实现 哈希表实现 有序 无序 -- 查找时间复杂度为O(1),非常快 空间消耗较大 空间消耗较 ...

  3. ASP.Net各个命名空间及作用

    (引用自hungerw的博客) 命名空间 描述 Microsoft.CSharp        支持C#语言编译和生成代码 System                            包含了基 ...

  4. Python中list,tuple,dict,set的区别和用法(转)

    原文地址:http://www.cnblogs.com/soaringEveryday/p/5044007.html Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个 ...

  5. idea中lombok安装

    项目中经常使用bean,entity等类,绝大部分数据类类中都需要get.set.toString.equals和hashCode方法,虽然eclipse和idea开发环境下都有自动生成的快捷方式,但 ...

  6. C#实现发送给QQ邮件

    最近在做一个通过点击忘记密码往用户邮箱中发邮件(邮件内容是一个超链接)点击进行修改的功能,发送原理,我们只是把邮件发送给smtp服务器,然后再由smtp服务器发送到邮箱,发送之前要校验一下. 1.微软 ...

  7. vue2.0跨域携带cookie和IE兼容

    /*vue-resource为了跨域获取到cookie做的操作*/ 1vue-resource跨域问题Vue.http.interceptors.push(function(request, next ...

  8. CSS day49

    前端基础之CSS CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CS ...

  9. 2Y - sort

    给你n个整数,请按从大到小的顺序输出其中前m大的数.  Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-5000 ...

  10. PAT 1088 三人行(20 分)(暴力破解+流程分析)

    1088 三人行(20 分) 子曰:"三人行,必有我师焉.择其善者而从之,其不善者而改之." 本题给定甲.乙.丙三个人的能力值关系为:甲的能力值确定是 2 位正整数:把甲的能力值的 ...