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 ...
随机推荐
- 判断用户 是用的电脑还是手机 判断 是安卓还是IOS
$(function(){ var Terminal = { // 辨别移动终端类型 platform: function () { var u = navigator.userAgent, app ...
- 模块移除 命令rmmod 的实现
// rmmod.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include < ...
- UMD模式的js
(function (root, factory) { if (typeof define === 'function' && define.amd) { // AMD define( ...
- ES5新增数组方法every()、some()、filter()、map()
JavaScript ES5标准中新增了一些Array方法,如every().some().filter().map().它们的出现使我们能够更加便利地操作数组,但对IE9以下浏览器的兼容性比较差.下 ...
- 无线渗透wep加密路由器
停掉网络服务 service network-manager stop 检查现在的环境适不适合使用airmon-ng airmon-ng check 杀死可能冲突的进程 开启网卡monitor模式 a ...
- kali域名解析错误解决
浏览器出现不能上网的的现象,推测是DNS解析有问题,想要修改DNS vim /etc/resolv.conf nameserver 202.96.134.133 nameserver 114.114. ...
- windows 下 wamp php单元测试工具PHPUnit的安装
phpunit下载网站 http://www.phpunit.cn/ 一.安装PHPUnit 1.选择版本 我用的是php版本是5.6.25 所以我选择的是PHPUnit 5.7 2.安装过程 为 P ...
- 可上下拖动且有浮沉动画的View
package com.ifenglian.superapp1; import android.animation.Animator;import android.animation.Animator ...
- 正则表达式pattern属性
你发现自己多久匆匆编写一些正则表达式验证一个特定的文本. 多亏了新的pattern属性,我们可以在标签处直接插入一个正则表达式. <form action="" method ...
- Dede文章标题长度修改
方法一.首先你要进入dedecms后台,系统——系统基本参数——其他选项——文档标题最大长度——在这修改为200或更大(其实200应该是足够了). 方法二.进入phpmyadmin,点击dede_ar ...