CodeForces 540E - Gerald and Giant Chess(数论)
给一个棋盘,需要从左上角走到右下角,有部分点不能走,求一共有多少种走法。
首先要知道从一个点A到另一个点B在没有障碍下有多少种走法。保证A在B的左上方,如图
一共需要走(X+Y)步(图中△x,△y),在其中选取X步走横向,Y步走竖向。所以一共有C(x+y, x)种走法。
把所有不能走的点排好序,对于每个点求出原点到该点的走法减去所有需要经过黑点的路径就是到该点的走法。
#include <bits/stdc++.h> using namespace std;
typedef long long ll;
const ll MOD = 1000000007; const int MAX_P = 200005; ll powMod(ll a, ll b, ll mod)
{
ll res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
} ll fac[MAX_P];
void getFact()
{
fac[0] = 1;
for (int i = 1; i <= 200000; ++i)
fac[i] = fac[i - 1] * i % MOD;
} ll Lucas(ll n, ll m, ll p)
{
ll res = 1;
while (n && m) {
ll a = n % p;
ll b = m % p;
if (a < b) return 0;
res = res * fac[a] * powMod(fac[b] * fac[a - b] % p, p - 2, p) % p;
n /= p;
m /= p;
}
return res;
} struct Point {
int x, y;
Point(int x, int y):x(x),y(y){}
Point(){}
bool operator<(const Point a) const
{
if (x == a.x) return y < a.y;
return x < a.x;
}
} p[2005]; ll way(Point a, Point b)
{
return Lucas(b.y - a.y + b.x - a.x, b.y - a.y, MOD);
} ll ans[2005];
int main()
{
int h, w, n;
getFact();
while (cin >> h >> w >> n) {
Point s(1, 1);
for (int i = 0; i < n; ++i) scanf("%d%d", &p[i].x, &p[i].y);
p[n].x = h, p[n].y = w;
sort(p, p + n);
for (int i = 0; i <= n; ++i) {
ans[i] = way(s, p[i]);
for (int j = 0; j < i; ++j) {
if (p[j].x <= p[i].x && p[j].y <= p[i].y) {
ans[i] = (ans[i] - way(p[j], p[i]) * ans[j]) % MOD;
}
}
}
ans[n] = (ans[n] + MOD) % MOD;
printf("%lld\n", ans[n]);
}
return 0;
}
CodeForces 540E - Gerald and Giant Chess(数论)的更多相关文章
- 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 ...
- Codeforces 559C Gerald and Giant Chess【组合数学】【DP】
LINK 题目大意 有一个wxh的网格,上面有n个黑点,问你从(1,1)走到(w,h)不经过任何黑点的方案数 思路 考虑容斥 先把所有黑点按照x值进行排序方便计算 \(dp_{i}\)表示从起点走到第 ...
- 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 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 ...
- 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 ...
- 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)
[题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...
随机推荐
- NGUI系列教程四(自定义Atlas,Font)
今天我们来看一下怎么自定义NGUIAtlas,制作属于自己风格的UI.第一部分:自定义 Atlas1 . 首先我们要准备一些图标素材,也就是我们的UI素材,将其导入到unity工程中.2. 全选我们需 ...
- joda jar日期处理类的学习
转载:http://www.open-open.com/lib/view/open1348032952724.html 任何企业应用程序都需要处理时间问题.应用程序需要知道当前的时间点和下一个时间点, ...
- CentOS PPTP配置LNMP+PPTP+FreeRADIUS+DaloRADIUS+流量控制
折腾了好几天,查阅了很多资料,终于搞定了,泪牛满面,下面记录详细操作过程!注:测试环境为CENTOS5.8 x86 安装PPTP 直接使用赵蓉的PPTP一键安装包即可 wget http://dl.z ...
- mysql function 与 procedure
Mysql 的 function 和 procedure 有啥区别呢 ? 网上搜索后说 function 有返回值, procedure 无返回值. 1.return 从function 的语法角度 ...
- django中外键关联表的查询随笔
django中,如果一个数据库中的表之间有外键的话可以方便的通过一个表查询到其相关表的数据.如有下面三个model:class Blog(models.Model): name = models ...
- 客户端动态调用cxf websevice 异常
三月 30, 2014 10:06:40 上午 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames 信息: Created clas ...
- [Gauss]POJ1753 Flip Game
题意:给4×4的棋盘的初始状态,b代表黑,w代表白. 要求变成全黑或者全白 最少需要几步. 简单的做法 可以暴搜 状压bfs 不再赘述 主要学习Gauss做法 同样是01方程组 用异或解 注意全黑或全 ...
- laravel code bright
Project RootLet’s start by taking a look at the root folder structure.• app/• bootstrap/• vendor/• p ...
- poj2029Get Many Persimmon Trees(最大矩阵和)
链接 类似最大矩阵 sum存下 枚举最大就是 #include <iostream> #include<cstdio> #include<cstring> #inc ...
- [Hadoop源码解读](一)MapReduce篇之InputFormat
平时我们写MapReduce程序的时候,在设置输入格式的时候,总会调用形如job.setInputFormatClass(KeyValueTextInputFormat.class);来保证输入文件按 ...