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 ...
随机推荐
- python学习笔记20(字符串格式化)
Python中内置有对字符串进行格式化的操作% 模板 格式化字符串时,Python使用一个字符串作为模板.模板中有格式符,这些格式符为真实值预留位置,并说明真实数值应该呈现的格式.Python用一个t ...
- zepto点击事件兼容pc和mobile
判断pc还是mobile,重写click事件 var CLICK='click'; (function browserRedirect() { var sUserAgent = navigator.u ...
- PAT-乙级-1003. 我要通过!(20)
1003. 我要通过!(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue “答案正确”是自动判题系统给出的最 ...
- 团体程序设计天梯赛-练习集L1-019. 谁先倒
L1-019. 谁先倒 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳 ...
- 团体程序设计天梯赛-练习集L1-013. 计算阶乘和
L1-013. 计算阶乘和 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对于给定的正整数N,需要你计算 S = 1! + 2 ...
- hdu 1233
最小生成树 本来挺简单 一个小错wa了好几遍 /************************************************************************* & ...
- uva 108
降维 枚举行累加 然后求单行上最大连续和 #include <iostream> #include <cstring> #include <cstdio> # ...
- 深入浅出 Java 8 Lambda 表达式
摘要:此篇文章主要介绍 Java8 Lambda 表达式产生的背景和用法,以及 Lambda 表达式与匿名类的不同等.本文系 OneAPM 工程师编译整理. Java 是一流的面向对象语言,除了部分简 ...
- WIN7+Ubuntu双系统,win7启动不了
在网上搜索了下,大多说的是因为重装引起的坏道, 我经过半天的搜索才找到了问题所在,首先看看下面连接的二楼大神给出的解决方案: https://forum.ubuntu.org.cn/viewtopic ...
- codeforces #313 div1 B
模拟判定就可以了 判定字符串是否相等用hash来判断 QAQ 值得一提的是一开始我交的时候T了 结果我将递归的顺序调整了一下就A了 (并不知道为什么 #include<cstdio> #i ...