Codeforces559C Gerald and Giant Chess
一道计数类\(DP\)
原题链接
我们可以先计算从左上角到右下角总的路径,再减去经过黑色方格的路径即是答案。
总路径数可以用组合数直接计算:\(C_{H+W-2}^{H-1}\)
因为从左上角到右下角必须走\(H+W-2\)步,而其中必须向右走\(H-1\)步,向下走\(W-1\)步,所以这就相当于是从\(H+W-2\)步中取出\(H-1\)步来向右走,剩下的向下走,这就是一个排列组合问题。
然后先按\(x,y\)递增的顺序对黑色方块进行排序,并设右下角为第\(n+1\)个黑色方块,第\(i\)个方块坐标为\((x_i,y_i)\)。
定义\(f[i]\)表示从左上角走到第\(i\)个黑色方块,且途中不经过其他黑色方块的路径总数。
于是有状态转移方程:
\(\qquad\qquad f[i]=C_{x_i+y_i-2}^{x_i-1}-\sum\limits_{j=1}^{i-1}f[j]\times C_{x_i-x_j+y_i-y_j}^{x_i-x_j},\text{且}x_i\geqslant x_j,y_i\geqslant y_j\)
其中第一个组合数是求从左上角到第\(i\)个黑色方块总的路径数,而这就需要减去这些路径中经过黑色方块的路径数,\(f[j]\)是从左上角到第\(j\)个黑色方块,且途中不经过其他黑色方块的路径数,而后面的组合数即是求从第\(j\)个黑色方块到第\(i\)个黑色方块的总路径数,两者满足乘法原理,乘起来就是从左上角到第\(i\)个黑色方块的路径中经过第\(j\)个黑色方格的路径数,而因为在\(j\)循环的过程中保证了第一个经过的黑色方格不同,所以计数时不会重复,直接累加减去即可。
最后组合数的计算可以先预处理阶乘和对应的逆元来\(O(1)\)计算。
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const int M = 2010;
const int mod = 1e9 + 7;
struct dd {
int x, y;
};
dd a[M];
int f[M];
ll inv[N << 1], fac[N << 1];
int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c<'0' || c>'9'; c = getchar())
p = (c == '-' || p) ? 1 : 0;
for (; c >= '0'&&c <= '9'; c = getchar())
x = x * 10 + (c - '0');
return p ? -x : x;
}
int comp(dd x, dd y)
{
if (x.x == y.x)
return x.y < y.y;
return x.x < y.x;
}
int ksm(int x, int y)
{
int s = 1;
for (; y; y >>= 1, x = 1LL * x*x%mod)
if (y & 1)
s = 1LL * s*x%mod;
return s;
}
int C(int x, int y)
{
return fac[y] * inv[x] % mod*inv[y - x] % mod;
}
int main()
{
int i, j, h, w, n, o;
h = re();
w = re();
n = re();
for (i = 1; i <= n; i++)
{
a[i].x = re();
a[i].y = re();
}
sort(a + 1, a + n + 1, comp);
for (fac[0] = i = 1, o = h + w; i <= o; i++)
fac[i] = fac[i - 1] * i%mod;
inv[o] = ksm(fac[o], mod - 2);
for (i = o - 1; i >= 0; i--)
inv[i] = inv[i + 1] * (i + 1) % mod;
a[n + 1].x = h;
a[n + 1].y = w;
for (i = 1; i <= n + 1; i++)
{
f[i] = C(a[i].x - 1, a[i].x + a[i].y - 2);
for (j = 1; j < i; j++)
if (a[j].x <= a[i].x&&a[j].y <= a[i].y)
f[i] = (f[i] - 1LL * f[j] * C(a[i].x - a[j].x, a[i].x + a[i].y - a[j].x - a[j].y) % mod) % mod;
}
printf("%d", (f[n + 1] + mod) % mod);
return 0;
}
Codeforces559C Gerald and Giant Chess的更多相关文章
- 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值就可以 ...
- 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 ...
- CF559C Gerald and Giant Chess
题意 C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input ...
- E. Gerald and Giant Chess
E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...
- 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 ...
- codeforces(559C)--C. Gerald and Giant Chess(组合数学)
C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)
[题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...
随机推荐
- Numpy知识(一)
先了解ndarray(一个多维数组)的生成. 第一种生成方法就是np.random.randn(n,m),这回生成一个形状是n*m的ndarray.如下图. 第二种生成方法:传入一个列表listDem ...
- 关于 uboot 的异常向量表
ldr r,address 表示将 address 地址中的内容存入 r0 中
- Alpine Linux 使用简介
https://blog.csdn.net/csdn_duomaomao/article/details/76152416
- webstocket 聊天
/** * 初始化socket **/ function initSocket(index_host){//端口号 if( !window.WebSocket ){ console.log(" ...
- Appium1.6 定位iOS元素和操作元素
元素定位方式 第一种:通过Appium1.6的Inspector来查看 具体安装方式前面的随笔已经介绍了:http://www.cnblogs.com/meitian/p/7360017.html ...
- java面试题:Linux
Q:Linux怎么查端口?端口被占用怎么办? netstat -ntulp | grep 2181//查看2181端口号 netstat -pan | grep 2181 //查看2181端口号 如下 ...
- javascript基础:函数参数与闭包问题
今天在写东西的时候,对函数参数的概念有些模糊,查阅相关资料后,在博客上记点笔记,方便日后复习. 首先,在js中函数参数并没有强语言中那么要求严格,他不介意传递进来多少个参数,也不在乎传进来的参数是什么 ...
- 微信小程序template模板与component组件的区别和使用
前言: 除了component,微信小程序中还有另一种组件化你的方式template模板,这两者之间的区别是,template主要是展示,方法则需要在调用的页面中定义.而component组件则有自己 ...
- 上海大都会赛 I Matrix Game(最大流)
At the start of the matrix game, we have an N x M matrix. Each grid has some balls.The grid in (i,j) ...
- spring BeanUtils 工具实现对象之间的copy
一般我们会开发中会遇到返回用户信息的时候,不需要返回密码或者其他参数,这时候我们需要重新定义一个VO类去除不需要的参数,将原对象copy到VO类中 使用spring的BeanUtils可以实现对象的c ...