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

首先要知道从一个点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. IE浏览器窗口合并

    百度经验:如何在IE上设置多窗口合并为单窗口(可切换)?

  2. 9.MVC框架开发(关于ActionResult的处理)

    1.在Action处理之后,必须有一个返回值,这个返回值必须继承自ActionResult的对象 2.ActionResult,实际就是服务器端响应给客户端的结果 ViewResult(返回视图结果) ...

  3. winform 项目获取app.config 中appSettings节点数据

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...

  4. JSP页面之${fn:}内置函数

    函数列表: 函数名 函数说明 使用举例 fn:contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, searchStrin ...

  5. 解决websphere在aix linux下日志乱码

    管理控制台--->服务器--->应用程序服务器--->server1--->java和进程管理--->进程定义--->java虚拟机--->将通用jvm参数设 ...

  6. Nginx完整配置说明

    http://blog.csdn.net/marising/article/details/3979493 可以参考如下的完整例子 http://wiki.codemongers.com/NginxF ...

  7. 浅谈HTTP中Get与Post的区别/HTTP协议与HTML表单(再谈GET与POST的区别)

    HTTP协议与HTML表单(再谈GET与POST的区别) GET方式在request-line中传送数据:POST方式在request-line及request-body中均可以传送数据. http: ...

  8. VC2008下CRichEditView加载RichEdit4.1版本(还有一些类似的文章)

         在之前的文章<RichEdit 各个版本介绍>中,写到RichEdit已经到达6.0版本了,而我们经常编程使用的却还是2.0,在vc6.0中甚至还使用1.0版本,更高的版本修复了 ...

  9. java中遍历List中的map问题

    List list = new ArrayList();Map map = null; while (rs.next()) { map = new HashMap(); map.put("f ...

  10. 【HDOJ】1045 Fire Net

    经典深搜.注意满足条件. #include <stdio.h> #include <string.h> #define MAXNUM 5 char map[MAXNUM][MA ...