传送门

令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. c# double 类型保留几位小数

    C 或 c :货币 D 或 d:十进制数 E 或 e:科学记数法(指数) F 或 f:定点 G 或 g:常规 N 或 n:数字 P 或 p:百分比 double.ToString("Nx & ...

  2. Codeforces Beta Round #42 (Div. 2)

    Codeforces Beta Round #42 (Div. 2) http://codeforces.com/contest/43 A #include<bits/stdc++.h> ...

  3. Centos + Maven + Jenkins

    下载 JDKwget --no-check-certificate --no-cookie --header "Cookie: oraclelicense=accept-secureback ...

  4. RocketMq顺序消费

    部分内容出处   https://www.jianshu.com/p/453c6e7ff81c rocketmq内部有4个默认的队里,在发送消息时,同一组的消息需要按照顺序,发送到相应的mq中,同一组 ...

  5. 将unitest整合和python发送测试报告

    废话少说先上代码 # -*- coding:UTF-8 -*- __autor__ = 'zhouli' __date__ = '2018/11/12 21:29' import unittest i ...

  6. 随机数、方法重载和System.out.println()的理解

    1.编写一个方法,使用以上算法生成指定数目(比如1000个)的随机数. package testradom; public class testradom { public static void m ...

  7. 小白鼠排队(map容器插入数据的四种方法)

    题目描述 N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的重量,要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色.帽子的颜色用“red”,“ ...

  8. java遍历当前会话所有Session

    //方法一:通过遍历的方法进行遍历 String FileName=""; HttpSession session=request.getSession();//获取session ...

  9. springmvc入门(1)

    一..springmvc框架 1.什么是springmvc springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合.springmvc是一个基于mv ...

  10. 关于java.io.IOException: HADOOP_HOME or hadoop.home.dir are not set.的问题

    报错如下: 300 [main] DEBUG org.apache.hadoop.util.Shell - Failed to detect a valid hadoop home directory ...