CF题目链接:点击打开题目
51Nod - 1486 题目链接:点击打开题目
C. Gerald and Giant Chess
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on anh × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?
The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.
Input
The first line of the input contains three integers: h, w, n — the sides of the board and the number of black cells (1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000).
Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w) — the number of the row and column of the i-th cell.
It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.
Output
Print a single line — the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo109 + 7.
Examples
input
3 4 2
2 2
2 3
output
2
input
100 100 3
15 16
16 15
99 88
output
545732279
1486 大大走格子
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题
收藏
关注
有一个h行w列的棋盘,里面有一些格子是不能走的,现在要求从左上角走到右下角的方案数。
Input
单组测试数据。
第一行有三个整数h, w, n(1 ≤ h, w ≤ 10^5, 1 ≤ n ≤ 2000),表示棋盘的行和列,还有不能走的格子的数目。
接下来n行描述格子,第i行有两个整数ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w),表示格子所在的行和列。
输入保证起点和终点不会有不能走的格子。
Output
输出答案对1000000007取余的结果。
Input示例
3 4 2
2 2
2 3
Output示例
2
正好51Nod帮我翻译了,就是那个意思。
首先一看肯定是组合数学的问题,然后还包括了逆元的内容。有若干不能走的点说明还包括了容斥原理,这里来一个一个说:
对与一个高h,宽w的格子,走到终点的方案数为C(h+w-2,h-2),但是这个图有不能走的地方,我们就要用容斥原理减去这些点的走法:我们先把这些点排一下顺序,第一关键字为x坐标,第二关键字为y坐标,把终点加入到最后一个点后面。
dp[ i ] 表示从起点到该点的走法数量,那么:
dp[ i ] = C(x+y-2,x-1)- ∑(dp[ j ] * C(a.x - b.x + a.y - b.y,a.x - b.x))(i < j)
最后我们更新到最后一个点(终点),这个点就是答案。
代码如下:
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL __int64
#define lld I64d //cf不能用longlong =。=
struct node
{
int x,y;
}point[2016];
const LL mod = 1000000007;
LL dp[2000+11];
LL fac[200000+11]; //阶乘表
LL inv[200000+11]; //阶乘逆元表
bool cmp(node a,node b)
{
if (a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
LL quick_mod(LL a,LL x)
{
LL ans = 1;
while (x)
{
if (x & 1)
ans = ans * a % mod;
a = a * a % mod;
x >>= 1;
}
return ans;
}
void getFac() //先打一个阶乘表
{
fac[0] = inv[0] = 1;
for (int i = 1 ; i <= 200000 ; i++)
{
fac[i] = fac[i-1] * i % mod;
inv[i] = quick_mod(fac[i] , mod-2);
}
}
LL C(int a,int b) //大组合数逆元
{
if (a < 0 || b < 0 || a < b) //改的时候漏了个 a<b 的条件,一直wa到死,泪奔 QAQ
return 0;
if (b == 0 || a == b)
return 1;
return fac[a] * (inv[b] * inv[a-b] % mod) % mod;
}
int main()
{
getFac();
int w,h,n;
scanf ("%d %d %d",&h,&w,&n);
for (int i = 0 ; i < n ; i++)
scanf ("%d %d",&point[i].x,&point[i].y);
point[n].x = h;
point[n].y = w;
n++;
sort (point , point+n , cmp);
for (int i = 0 ; i < n ; i++)
{
dp[i] = C(point[i].x+point[i].y-2 , point[i].x-1);
for (int j = 0 ; j < i ; j++)
{
dp[i] -= C(point[i].x - point[j].x + point[i].y - point[j].y , point[i].x - point[j].x) * dp[j] % mod;
//这里要取余,否则减到后面数太大
dp[i] = (dp[i] % mod + mod) % mod;
}
}
// LL ans = C(w+h-2 , w-2) - dp[n-1]; //要搞清楚dp数组的含义
// printf ("%lld\n",(ans%mod + mod) % mod);
printf ("%lld\n",dp[n-1]);
return 0;
}