给一个棋盘,需要从左上角走到右下角,有部分点不能走,求一共有多少种走法。

首先要知道从一个点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(数论)的更多相关文章

  1. 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 ...

  2. Codeforces 559C Gerald and Giant Chess【组合数学】【DP】

    LINK 题目大意 有一个wxh的网格,上面有n个黑点,问你从(1,1)走到(w,h)不经过任何黑点的方案数 思路 考虑容斥 先把所有黑点按照x值进行排序方便计算 \(dp_{i}\)表示从起点走到第 ...

  3. 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的网格,让你 ...

  4. 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 ...

  5. 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 ...

  6. Gerald and Giant Chess

    Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. CF559C Gerald and Giant Chess

    题意 C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. E. Gerald and Giant Chess

    E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...

  9. 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)

    [题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...

随机推荐

  1. 【filter】springmvc web.xml

    1.filter用于拦截用户请求,在服务器作出响应前,可以在拦截后修改request和response,这样实现很多开发者想得到的功能. 2.filter实现 ×编写一个继承Filter接口的类 ×在 ...

  2. 洛谷1508 Likecloud-吃、吃、吃

    题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏 ...

  3. add a path cgi-bin to asp.net mvc

    1.简单,但是会丢失请求数据 protected void Application_BeginRequest() { string url = HttpContext.Current.Request. ...

  4. windows10 Build 10074版本上手体验(二)

  5. 视频边下边播--缓存播放数据流-b

    google搜索“iOS视频变下边播”,有好几篇博客写到了实现方法,其实只有一篇,其他都是copy的,不过他们都是使用的本地代理服务器的方式. 原理很简单,但是缺点也很明显,需要自己写一个本地代理服务 ...

  6. android打造万能的适配器(转)

    荒废了两天,今天与大家分享一个ListView的适配器 前段时间在学习慕课网的视频,觉得这种实现方式较好,便记录了下来,最近的项目中也使用了多次,节省了大量的代码,特此拿来与大家分享一下. 还是先看图 ...

  7. Codeforces Round #208 (Div. 2)

    A - Dima and Continuous Line 水题:直接模拟: #include<cstdio> #define maxn 1005 using namespace std; ...

  8. Java 8 vs. Scala(二):Stream vs. Collection

    [编者按]在之前文章中,我们介绍了 Java 8和Scala的Lambda表达式对比.在本文,将进行 Hussachai Puripunpinyo Java 和 Scala 对比三部曲的第二部分,主要 ...

  9. *[topcoder]LittleElephantAndIntervalsDiv1

    http://community.topcoder.com/stat?c=problem_statement&pm=12822&rd=15707 第一次用C++提交,艰辛.首先想到可以 ...

  10. 158. Read N Characters Given Read4 II - Call multiple times

    题目: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the ...