A Simple Chess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1784 Accepted Submission(s): 477
(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.
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) .
1 1 0 3 3 0 4 4 1 2 1 4 4 1 3 2 7 10 2 1 2 7 1
Case #1: 1 Case #2: 0 Case #3: 2 Case #4: 1 Case #5: 5
题目大意:
一个棋盘中有一个棋子在(1,1)点,棋子只能走日字型并且只能向右下方走,并且棋盘上有x个障碍点,棋子不能落在障碍点上,问走到(n,m)点有多少种方法。
解题思路:
先考虑没有障碍的情况:棋子从(1,1)到(3,2)与(2,3)只有一种走法,到(5,3),(3,5)有一种走法,到(4,4)有两种走法。
(5,3)只能由(3,2)到达,(3,5)只能由(2,3)到达,(4,4)这个点是由(3,2)与(2,3)这两点到达的,因此走的方法为(3,2)与(2,3)两点方法的和。
由此递推,每个点到达的方法数是由上面两个可到达点的方数的和。因此是一个杨辉三角。
1
1 1
1 2 1
1 3 3 1
............................................
同时注意到题目中n,m的范围是10^18,因此无法通过开数组递推得到每个点的方法数。
这时候就想到杨辉三角每个点的公式为C(n,m)(n为层数,m为第几个)
但是直接计算会导致数值过大,因此就要用到Lucas算法,之后就能得到结果。
之后考虑障碍物的情况:
每个在棋子能到达点上的障碍物都会对后面的结果产生影响,因此要先考虑每个障碍物对后面的点的影响。
每个上层的障碍物在下层的每个点中出现的次数又是一个以该点为顶点的小杨辉三角,因此去掉上层的点(n,m)对下层的点(N,M)的影响为:C(N,M)-C(N-n,M-m)*C(n,m)。(若M-m小于0则代表上层的点不会对下层该点产生影响)
将所有障碍物按照在杨辉三角中的层数进行排序,层数小的在前面。
之后遍历所有的障碍点,对每个后面层能产生障碍的点进行一次计算,把后面障碍点的方法数重新计算。
因为障碍点数量为100因此O(n^2)的复杂度可行。
然后再遍历每个障碍点对终点进行一次计算。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<math.h>
#include<map>
using namespace std;
long long F[110200];
map<long long,map<long long,int>>vis;
struct point
{
long long x,y,num,num2;
point():num2(0){}
};
point pp[105];
bool cmp(point a,point b)
{
if(a.x<b.x)return true;
if(a.x==b.x&&a.y<=b.y)return true;
return false;
}
void init(long long p)
{
F[0] = 1;
for(long long i = 1;i < 110200;i++)
F[i] = F[i-1]*i%p;
}
long long inv(long long a,long long m)
{
if(a == 1)return 1;
return inv(m%a,m)*(m-m/a)%m;
}
long long Lucas(long long n,long long m,long long p)
{
long long ans = 1;
while(n&&m)
{
long long a = n%p;
long long b = m%p;
if(a < b)return 0;
ans = ans*F[a]%p*inv(F[b]*F[a-b]%p,p)%p;
n /= p;
m /= p;
}
return ans;
}
bool judge(long long n,long long m)//判断该点是否在杨辉三角上,如果不在直接输出0
{
if((n+m)%3!=0)return false;
else
{
long long hang=(n+m)/3;
if((n-hang)<0||(n-hang)>hang)return false;
return true;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
long long n,m,p,k;
int t=1;
p=110119;
init(p);
while(scanf("%lld%lld%lld",&n,&m,&k)!=EOF)
{
vis.clear();
int len=0,flag=0;
while(k--)
{
long long x,y;
scanf("%lld%lld",&x,&y);
if((x==1&&y==1)||(x==n&&y==m))
flag=1;
if(judge(x-1,y-1))
{
if(vis[x][y]==0)
{
pp[len].x=x;
pp[len].y=y;
pp[len++].num=Lucas((x+y-2)/3,x-(x+y-2)/3-1,p);
vis[x][y]=1;
}
}
}
sort(pp,pp+len,cmp);
for(int i=0;i<len;i++)
for(int j=i+1;j<len;j++)
{
if((pp[j].x>pp[i].x)&&(pp[j].y>pp[i].y))
{
long long xx=pp[j].x-pp[i].x+1,yy=pp[j].y-pp[i].y+1;
if(judge(xx-1,yy-1))
{
pp[j].num=(pp[j].num-(Lucas((xx+yy-2)/3,xx-(xx+yy-2)/3-1,p)*pp[i].num)%p+p)%p;
}
}
}
if(flag==1||!judge(n-1,m-1))
printf("Case #%d: 0\n",t);
else
{
long long hang=(n+m-2)/3;
long long lie=n-hang-1;
long long num=Lucas(hang,lie,p);
for(int i=0;i<len;i++)
{
if((n>pp[i].x)&&(m>pp[i].y))
{
long long xx=n-pp[i].x+1,yy=m-pp[i].y+1;
if(judge(xx-1,yy-1))
{
num=(num-(Lucas((xx+yy-2)/3,xx-(xx+yy-2)/3-1,p)*pp[i].num)%p+p)%p;
}
}
}
printf("Case #%d: %lld\n",t,num);
}
t++;
}
return 0;
}