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 ...
随机推荐
- spark LBFGS 设置参数
http://blog.csdn.net/bon_mot/article/details/72461318
- js正则表达式子校验
//正则表达式校验new RegExp(/^[1-9]\d{4,8}$/,"g").test(1234);//执行一个字符串所表达的方法 eval(this['字符串']) 正则表 ...
- pandas 常用清洗数据(一)
数据源获取: https://www.kaggle.com/datasets 1. Look at the some basic stats for the ‘imdb_score’ column: ...
- numpy 矩阵变换 reshape ravel flatten
1. 两者的区别在于返回拷贝(copy)还是返回视图(view),numpy.flatten()返回一份拷贝,对拷贝所做的修改不会影响(reflects)原始矩阵,而numpy.ravel()返回的是 ...
- 04_web基础(三)之进一步理解web
08.BS和CS与Tomcat详细介绍 1.cs与bs架构的简介及区别 CS和BS是软件架构模式:C/S: Client/Server :客户端/服务端架构:B/S: Browser/Server:浏 ...
- JDK1.8 HashMap 扩容 对链表(长度小于默认的8)处理时重新定位的过程
关于HashMap的扩容过程,请参考源码或百度. 我想记录的是1.8 HashMap扩容是对链表中节点的Hash计算分析. 对术语先明确一下: hash计算指的确定节点在table[index]中的链 ...
- ORA-30926: 无法在源表中获得一组稳定的行ORA-06512: 在 "STG.FP_MO_SPLIT", line 1562 临时
- meta标签的使用
meta标签是html标记head区的一个关键标签,它位于HTML文档的<head>和<title>之间(有些也不是在<head>和<title>之间) ...
- Java 管道PipedInputStream PipedOutStream PipedReader PipedWriter
java中的管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据.一个线程发送数据到输出管道,另外一个线程从输入管道中读取数据.通过使用管道,实现不同线程间的通信,而不必借助类似 ...
- 数据库与ORM
一.数据库的配置 1 django默认支持sqlite,mysql, oracle,postgresql数据库. <1> sqlite django默认使用sqlite的数据库,默认自带 ...