A Simple Chess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2015 Accepted Submission(s): 545
Problem 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.
(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) .
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 1
Sample Output
Case #1: 1 Case #2: 0 Case #3: 2 Case #4: 1 Case #5: 5
Author
UESTC
Source
给定一个棋盘,你要从(1,1)开始走到(n,m)每次只能走右右下或下下右,然后其中有些地方有障碍物,问你有多少种方案可以走到终点。
题解:
首先如果2点是可达的,那么(x2-x1+y2-y1)为3的倍数,并且横纵位移不能超过两倍。
然后我们假设从(x1,y1)到(x2,y2)要走a个右右下b个下下右,我们可以把ab解出来,方案数就是C(a+b,a)。这里的a,b比较大但是MOD比较小,所以用LUCAS定理。
总的方案数我们已经搞定了,剩下的就是障碍,用容斥搞一下就好了。
终点可能有障碍物要特判一下。
PS:100个障碍物居然不超时。。果然数据还是很水的。
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <ctime>
#define fur(i,a,b) for(int i=(a);i<=(b);i++)
#define furr(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
typedef long long ll;
const ll mod=110119;
inline void gn(long long&x){
int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
// (づ°ω°)づe★
//-----------------------------------------------------------------
vector<pair<ll,ll> >p;
ll n,m;
ll ans;
int r;
inline long long mod_pow(int a,int n,int p)
{
long long ret=1;
long long A=a;
while(n)
{
if (n & 1)
ret=(ret*A)%p;
A=(A*A)%p;
n>>=1;
}
return ret;
}
long long factorial[mod+100];
void init(long long p)
{
factorial[0] = 1;
for(int i = 1;i <= p;i++)
factorial[i] = factorial[i-1]*i%p;
}
inline long long Lucas(long long a,long long k,long long p)
{
long long re = 1;
while(a && k)
{
long long aa = a%p;long long bb = k%p;
if(aa < bb) return 0;
re = re*factorial[aa]*mod_pow(factorial[bb]*factorial[aa-bb]%p,p-2,p)%p;
a /= p;
k /= p;
}
return re;
}
inline bool con(ll x1, ll y1, ll x2, ll y2){ //是否可从x1,y1到x2,y2
if(x2<=x1||y2<=y1) return false;
if((x2-x1+y2-y1)%3!=0) return false;
ll dx = x2-x1, dy = y2-y1;
if(dx>2ll*dy || dy>2ll*dx) return false;
return true;
}
inline ll solve(ll x1, ll y1, ll x2, ll y2){
ll nn = (x2-x1+y2-y1)/3ll, mm = (2ll*(y2-y1)-(x2-x1))/3ll;
return Lucas(nn,mm,mod) % mod;
}//从x1,y1到x2,y2的方案数
void dfs(int id, int v, int cnt){
ll vv = ( (v%mod) * (solve(p[id].first,p[id].second,n,m)%mod) ) % mod;
if(cnt & 1) ans = (ans + mod - vv) % mod;
else ans = (ans + vv) % mod;
for(int i = id+1;i < p.size(); ++i){
if(con(p[id].first,p[id].second,p[i].first,p[i].second)){
ll vvv = v * solve(p[id].first,p[id].second,p[i].first,p[i].second) % mod;
dfs(i,vvv,cnt+1);
}
}
}
int main(){
init(mod);
int kase = 0;
while(scanf("%I64d %I64d %d",&n,&m,&r) != EOF){
ll a,b;
p.clear();
fur(i,1,r){
gn(a);gn(b);
if(con(1,1,a,b) && con(a,b,n,m))
p.push_back(make_pair(a,b));
}
if(n == 1 && m == 1){
printf("Case #%d: 1\n",++kase);
continue;
}
if((r>0 && (a == n && b == m) ) || !con(1,1,n,m) ){
printf("Case #%d: 0\n",++kase);
continue;
}
sort(p.begin(),p.end());
ans = solve(1ll,1ll,n,m) % mod;
int sz = p.size();
for(int i = 0; i < sz; ++i){
ll v = solve(1ll,1ll,p[i].first,p[i].second) % mod;
dfs(i,v,1);
}
printf("Case #%d: %I64d\n",++kase,ans);
}
return 0;
}