A Simple Chess
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5794
Description
There is a n×m board, a chess want to go to the position (n,m) from the position (1,1). The chess is able to go to position (x2,y2) from the position (x1,y1), only and if only x1,y1,x2,y2 is satisfied that (x2−x1)2+(y2−y1)2=5, x2>x1, y2>y1. Unfortunately, there are some obstacles on the board. And the chess never can stay on the grid where has a obstacle. I want you to tell me, There are how may ways the chess can achieve its goal.Input
The input consists of multiple test cases. For each test case: The first line is three integers, n,m,r,(1≤n,m≤1018,0≤r≤100), denoting the height of the board, the weight of the board, and the number of the obstacles on the board. Then follow r lines, each lines have two integers, x,y(1≤x≤n,1≤y≤m), denoting the position of the obstacles. please note there aren't never a obstacles at position (1,1).Output
For each test case,output a single line "Case #x: y", where x is the case number, starting from 1. And y is the answer after module 110119.Sample Input
1 1 0 3 3 0 4 4 1 2 1 4 4 1 3 2 7 10 2 1 2 7 1Sample Output
Case #1: 1 Case #2: 0 Case #3: 2 Case #4: 1 Case #5: 5Source
2016 Multi-University Training Contest 6##题意: 在n*m的棋盘上,从左上角(1,1)走到右下角(n,m)有多少条路线. 每次只能向右下方走"L"型路线. (x,y) => (x+1,y+2) or (x+2, y+1); 棋盘上有n个(n<=1000)不能达到的障碍位置.
##题解: 问题一:如何计算路线数 如果不存在任何障碍,从点(x1,y1)到点(x2,y2): ①. 不能到达:由于走的是"L"型路线(类似象棋的马),所以有可能到不了. 每走一步实际上是移动了三个单位(包括上下方向),而起点到终点共有(x2-x1) + (y2-y1)个单位. 所以能走到的充分必要条件是:((x2-x1)+(y2-y1)) % 3 == 0 ; 且终点在起点的左下方(x2>x1 && y2>y1). ②. 如果能到达:走的步数是 step = (x2+y2-x1-y1) / 3. 走法有 "L"型{(x,y)=>(x+2,y+1)} 和 "倒L"型{(x,y)=>(x+1,y+2)} 两种. 无论怎么走都至少会在上下两方向上都减1. 所以上下两方向各剩下 s1=x2-x1-step 和 s2=y2-y1-step 个单位. 再用"L"型和"倒L"型去分配这剩下的单位. 即 C(s1+s2, s1) 或 C(s1+s2, s2). (另解:先看成往右下走一格,再抉择往右还是往下.) ③. 由于坐标范围较大,而取模的p=110119较小且为质数. 所以这里用lucas公式来求组合数. (一般的:数较小且mod较大时用逆元求组合数,数较大且mod较小时用lucas求组合数) (由于上述公式中s1 s2有可能为负数,所以lucas里面一定要对负数组合数作特判) (论完美模版的重要性).
问题二:如何考虑障碍物 容斥 + DP:坐标范围较大,只能对障碍位置进行dp: dp[i]: 从(1,1)到障碍i且不经过其他障碍的路线数. 转移方程: dp[i] = (起点到障碍i的路线) - Σ(dp[j] * (障碍j到障碍i的路线)) (其中j为位于障碍i左上方的障碍,即j可以通过合法路径到达i. 因此处理时先对障碍坐标排序).
类似题目对比总结: 至今为止做过五个类似的题: 1. CF-560E [Gerald and Giant Chess](http://www.codeforces.com/problemset/problem/560/E) 每次只能往右或下走,方案数比较好求,逆元求组合数(坐标较小). 2. UVALive-6916 [Punching Robot](http://acm.hust.edu.cn/vjudge/contest/97671#problem/K) 每次只能往右或下走,方案数比较好求,Lucas求组合数(坐标较大). 3. Gym-100379J [Move the (p, q)-knight to the corner!](http://acm.hust.edu.cn/vjudge/contest/87224#problem/H) 这个题跟本题几乎一样,走"类L"型路线,Lucas求组合数. 4. NOIP1997-普及组 [街道](http://oj.tsinsen.com/A1110) 这个题是坐标较小(50*50),障碍是一个矩形区域,对非障碍位置DP+高精度即可. 5. HDU-5794 [本题](http://acm.hdu.edu.cn/showproblem.php?pid=5794) 走"L"型路线,Lucas求组合数.
##代码: ``` cpp #include