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 ...
随机推荐
- Sybase数据库异常紧急恢复
现象:Error 926 Severity Level 14 Error Message Text Database 'xx' cannot be opened - it has been ma ...
- Sybase ASE无响应的又一个情况
昨天下午,客户那边的系统管理员给我电话,说有套系统的SYBASE数据库最近有点怪,总是时不时莫名其妙地就忽然卡死,有可能一下子就自动恢复了,也有可能后面一直卡住,只好重启.根据客户反映的状况,初步判断 ...
- Automotive Security的一些资料和心得(2):Cryptography
1. Security Goal - Confidentiality - Integrity - Availability - Authenticity - Non-repudiation - Aut ...
- hadoop-streaming 配置之---参数分割
map: -D stream.map.output.field.separator=. 定义mapoutput字段的分隔符为. 用户可以自定义分隔符(除了默认的tab) -D stream.num.m ...
- Spring3事务管理——使用@Transactional 注解(转)
文章地址:http://my.oschina.net/guanzhenxing/blog/214228
- css3 旋转出现动画
@-moz-keyframes daf{ 0% { -moz-transform: rotate(-360deg) scale(0.2); -webkit-transform: rotate(-360 ...
- Ubuntu之网络配置
一.配置大概分三类:通过配置文件配置.通过命令配置.通过图形化的网络连接菜单配置. 拨号无线等的没条件实验,不涉及. 主要文件:/etc/network/interfaces,这里是IP.网关.掩码等 ...
- android usb host 读写USB设备
自android3.1以后android增加了操作USB设备的API. 官网地址:http://developer.android.com/guide/topics/connectivity/usb/ ...
- SQL server数据类型int、bigint、smallint、tinyint
1. 整数类型 int.bigint.smallint.tinyint 数据类型 范围 存储 bigint -2^63 (-9,223,372,036,854,775,808) 到 2^63-1 (9 ...
- JavaScript实现命令行交互
原文地址: http://www.cnblogs.com/liaoyu/p/js-terminal.html 周末闲着想试试用 JavaScript 模拟命令行交互的功能,希望达到的几个功能点如下: ...