CF 560e Gerald and Giant Chess
题意:在h×w的棋盘中从左上角走到右下角,只能向右或向下走,有n个点不可以经过,一共有多少种方案。
解法:dp。先对点按横坐标排序(横坐标相等按纵坐标,也可以反过来)dp[i]表示不经过其他非法点走到第i个非法点的路径数,则有dp方程:dp[i] = c(point[i].x + point[i].y - 2, point[i].x - 1) - Σj = 0...i - 1 dp[j] * c(point[i].x - point[j].x + point[i].y - point[j].y, point[i].x - point[j].x),c表示组合数,c(point[i].x + point[i].y - 2, point[i].x - 1)表示从起点到该非法点i的所有路径,再减去在该点之前的每个点j,从起点到点j的路径数乘以从点j到点i的路径数,即为所求,将终点加入点集中,则dp[n]为答案。
而对于组合数的求法,这道题无法用杨辉三角打表,所以用阶乘来求,即c(n, m) = n! / (m! * (n - m)!),但由于有取模运算,不能直接做除法,所以转化为乘以分母的逆元,m = n mod p的逆元为mp-2。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
struct node
{
LL x, y;
bool operator < (const node &tmp) const
{
if(x == tmp.x)
return y < tmp.y;
return x < tmp.x;
}
}point[2005];
const LL mod = 1e9 + 7;
LL jc[200005];//阶乘
void init()
{
jc[0] = 1;
for(int i = 1; i < 200005; i++)
{
jc[i] = (jc[i - 1] * i) % mod;
}
}
LL Pow(LL n)//求逆元
{
n %= mod;
LL x = 1e9 + 5;
LL res = 1;
while(x)
{
if(x & 1)
res = res * n % mod;
n = n * n % mod;
x >>= 1;
}
return res;
}
LL c(int x, int y)
{
if(x < 0)
return 0;
if(y < 0)
return 0;
if(y > x)
return 0;
//不判断以上三个条件会RE
return jc[x] * Pow(jc[y] * jc[x - y] % mod) % mod;
}
LL dp[2005];
int main()
{
init();
int h, w, n;
while(~scanf("%d%d%d", &h, &w, &n))
{
memset(dp, 0, sizeof dp);
for(int i = 0; i < n; i++)
{
cin >> point[i].x >> point[i].y;
}
point[n].x = h, point[n].y = w;
sort(point, point + n);
LL ans = 0;
for(int i = 0; i <= n; i++)
{
LL tmp = c(point[i].x + point[i].y - 2, point[i].x - 1);
for(int j = 0; j < i; j++)
{
tmp += mod - (dp[j] * c(point[i].x - point[j].x + point[i].y - point[j].y, point[i].x - point[j].x) % mod);
tmp %= mod;
}
dp[i] = tmp;
}
cout << dp[n] << endl;
}
return 0;
}
CF不能交lld真蛋疼……
CF 560e Gerald and Giant Chess的更多相关文章
- CF 559C - Gerald and Giant Chess (组合计数)
\(C_{x+y}^y\)的公式,DP容斥删多余贡献. #include <cstdio> #include <iostream> #include <cstring&g ...
- 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 ...
随机推荐
- go语言使用protobuf
网上为什么充斥着大量几乎一模一样而且不正确的教程??? 妈的打开一个关于golang和protobuf的教程,无非都是protobuf多么多么牛逼,xml多么多么傻逼,然后就是怎么安装protobuf ...
- EXTJS 4.2 资料 控件之 xtype: "fieldcontainer",追加html
{ xtype: "fieldcontainer", layout: "hbox", items: [ { fieldLabel: '素材目录', name: ...
- .net google calendar
https://developers.google.com/gdata/client-cs http://www.codeproject.com/Articles/64474/How-to-Read- ...
- javascript eval 执行过程
当执行eval时,会执行如下过程 eval(x): 1.如果 x的类型不是string,那么会return x; 2.把x转换成 ecmascript 代码.如果转换失败,责抛出SyntaxError ...
- iOS定位坐标转换工具-b
坐标系介绍 首先介绍一下目前的定位坐标系统1.地球坐标 :( 代号:GPS.WGS84 )--- 有W就是世界通用的也就是原始坐标体系,这是国际公认的世界标准坐标体系: 使用 WGS84 坐标系统的产 ...
- bnuoj 29373 Key Logger(模拟双向队列)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=29373 [题意]:模拟光标输入 [题解]:用双向列表模拟实现,这里用其他模拟会超时,注意内存的释放 ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- 解决mysql中表字符集gbk,列字符集Latin1,python查询乱码问题
最近在公司碰到一个异常蛋疼的情况,mysql数据库中,数据库和表的字符集都是'gbk',但是列的字符集却是'latin1',于是蛋疼的事情出现了. 无论我连接字符串的`charset`设置为`gbk` ...
- [转载]test后跟je
今天俺也用OD(OllyDbg)反汇编了个小软件,其中里面有下面两条指令: 没太明白什么意思,google一下,在看雪论坛上发现了一个大虾的解释很详细,记录一下: 1.test a,b 是a与b相与的 ...
- [转载]jQuery 1.9 移除了 $.browser 的替代方法获取浏览器类型
jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support . 在更新的 2.0 版本中,将不再支持 IE 6/7/8. ...