A Simple Chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2597    Accepted Submission(s): 691

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.
 
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 1
 
Sample Output
Case #1: 1
Case #2: 0
Case #3: 2
Case #4: 1
Case #5: 5
 
Author
UESTC
 
Source
 
 
/**
题目:A Simple Chess
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794
题意:从(1,1)开始出发,每一步从(x1,y1)到达(x2,y2)满足(x2−x1)^2+(y2−y1)^2=5, x2>x1,y2>y1;
其实就是走日字。而且是往(n,m)方向走的日字。还有r个障碍物,障碍物不可以到达。求(1,1)到(n,m)的路径条数。
思路:容斥+Lucas 如果没有障碍物:那么每一次可以选择从(x1,y1)到达(x1+2,y1+1)或者(x1+1,y1+2);
那么设选择了x次(x1+2,y1+1),y次(x1+1,y1+2)
那么: x1+2*x+y = n; => 2*x+y = n-x1;
x+2*y+y1 = m; 2*y+x = m-y1; x = (2*n-2*x1-m+y1)/3;
y = (2*m-2*y1-n+x1)/3; 说明如果2*n-2*x1-m+y1或者2*m-2*y1-n+x1不是3的倍数,(x,y都必须非负整数),那么无法到达。 否则路径条数为:C(x+y,x); 存在障碍物:
假设只有一个障碍物,那么用总的路径条数sum-经过这一个障碍物的路径条数dp[1]。
假设存在两个障碍物,那么sum-经过的第一个障碍物为编号1的路径条数-经过的第一个障碍物为编号2的路径条数。(注意:第一个!!!) 经过的第一个障碍物为编号1的路径条数:从(1,1)到达(x1,y1)的路径条数乘以(x1,y1)到达(n,m)的路径条数。
经过的第一个障碍物为编号2的路径条数:(从(1,1)到达(x2,y2)的路径条数-从(1,1)到达(x1,y1)然后从(x1,y1)到达(x2,y2)的路径条数)
乘以 从(x2,y2)到达(n,m)的路径条数。 当多个障碍物时,方法同上处理。 处理c(x+y,x)%mod用Lucas定理。
*/ #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int maxn = 1e6+;
const int mod = ;
LL f[mod+];///阶乘。
LL inv[mod+];///逆元
LL exgcd(LL a, LL b, LL &x, LL &y)///扩展欧几里得;
{
if (!b)
{
x = ;
y = ;
return a;
}
LL gcd = exgcd(b, a % b, x, y);
LL t = x;
x = y;
y = t - (a / b) * x;
return gcd;
}
LL inverse(LL num, LL mod)///求逆元;
{
LL x, y;
exgcd(num, mod, x, y);
return (x % mod + mod) % mod;
}
void init()///如果mod小,那么可以线性筛逆元。
{
inv[] = ;
for(int i = ; i < mod; i++){
inv[i] = (mod-mod/i)*inv[mod%i]%mod;
}
f[] = ;
for(int i = ; i < mod; i++){///预处理阶乘。
f[i] = f[i-]*i%mod;
}
}
LL mult(LL a,LL b,LL p)///解决 大数a*b%p溢出long long 的方法;
{
LL ans=;
while(b)
{
if(b&)
ans=(ans+a)%p;
b>>=;
a=(a+a)%p;
}
return ans;
}
LL C(LL a, LL b, LL mod)///实现C(n,m)%p
{ if (b > a)
return ;
return mult(mult(f[a],inv[f[b]],mod),inv[f[a-b]],mod);/// a!/(b!*(a-b)!);
}
LL lucas(LL n, LL m, LL p)///卢卡斯定理实现;c(n,m)%p;
{
if (m == )
return ;
return mult(C(n % p, m % p, p),lucas(n / p, m / p, p),p);
}
LL solve(LL x1,LL y1,LL n,LL m)
{
if((*n-*x1-m+y1)%!=) return ;
if((*m-*y1-n+x1)%!=) return ;
LL x = (*n-*x1-m+y1)/;
LL y = (*m-*y1-n+x1)/;
if(x<||y<) return ;
return lucas(x+y,y,mod)%mod;
}
LL n, m, r;
struct node
{
LL x, y;
bool operator < (const node&k)const{
if(x==k.x) return y<k.y;
return x<k.x;
}
}t[];
LL ans[];
int main()
{
int cas = ;
init();///初始化逆元。
while(scanf("%lld%lld%lld",&n,&m,&r)!=EOF)
{
for(int i = ; i < r; i++){
scanf("%lld%lld",&t[i].x,&t[i].y);
}
sort(t,t+r); LL sum = solve(,,n,m);
for(int i = ; i < r; i++){
ans[i] = solve(,,t[i].x,t[i].y);
for(int j = ; j < i; j++){
ans[i] = (ans[i]-ans[j]*solve(t[j].x,t[j].y,t[i].x,t[i].y)%mod+mod)%mod;
}
}
//cout<<"sum = "<<sum<<endl;
//cout<<"ans[0] = "<<ans[0]<<endl;
for(int i = ; i < r; i++){
sum = (sum-ans[i]*solve(t[i].x,t[i].y,n,m)%mod+mod)%mod;
}
printf("Case #%d: %lld\n",cas++,sum);
}
return ;
}

hdu5794 A Simple Chess 容斥+Lucas 从(1,1)开始出发,每一步从(x1,y1)到达(x2,y2)满足(x2−x1)^2+(y2−y1)^2=5, x2>x1,y2>y1; 其实就是走日字。而且是往(n,m)方向走的日字。还有r个障碍物,障碍物不可以到达。求(1,1)到(n,m)的路径条数。的更多相关文章

  1. hdu-5794 A Simple Chess(容斥+lucas+dp)

    题目链接: A Simple Chess Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

  2. HDU5794 A Simple Chess 容斥+lucas

    分析:转自http://blog.csdn.net/mengzhengnan/article/details/47031777 一点感想:其实这个题应该是可以想到的,但是赛场上并不会 dp[i]的定义 ...

  3. 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)

    [题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...

  4. Codeforces Round #258 (Div. 2) 容斥+Lucas

    题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...

  5. A Simple Chess---hdu5794(容斥+Lucas)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794 题意:给你一个n*m的网格,问从(1, 1)走到(n, m)的方案数是多少,其中有r ...

  6. Codeforces.348D.Turtles(容斥 LGV定理 DP)

    题目链接 \(Description\) 给定\(n*m\)的网格,有些格子不能走.求有多少种从\((1,1)\)走到\((n,m)\)的两条不相交路径. \(n,m\leq 3000\). \(So ...

  7. hdu1695(莫比乌斯)或欧拉函数+容斥

    题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...

  8. HDU - 5977 Garden of Eden (树形dp+容斥)

    题意:一棵树上有n(n<=50000)个结点,结点有k(k<=10)种颜色,问树上总共有多少条包含所有颜色的路径. 我最初的想法是树形状压dp,设dp[u][S]为以结点u为根的包含颜色集 ...

  9. 【UOJ#390】【UNR#3】百鸽笼(动态规划,容斥)

    [UOJ#390][UNR#3]百鸽笼(动态规划,容斥) 题面 UOJ 题解 发现这就是题解里说的:"火山喷发概率问题"(大雾 考虑如果是暴力的话,你需要记录下当前每一个位置的鸽笼 ...

随机推荐

  1. URL Schemes(转载)

    URL Schemes 应用在 iOS 上已经很久了.对于使用者来说,在沙盒机制下的 iOS 中,如果想做到一定程度上的自动化就不可避免地要用到 URL Schemes.但因为 URL Schemes ...

  2. 再谈EditText只能输入金额

    上次写了一篇EditText只能输入金额的博客,后来发现一个bug,当还未输入数字的情况下输入小数点程序就崩了,我去测了一下支付宝,看看会怎么样,我先输入小数点,程序正常,我再输入数字,可以正常输入, ...

  3. 【spring boot】spring cloud下spring boot微服务启动没有报错,但是访问访问不到

    spring cloud下spring boot微服务启动没有报错,但是访问访问不到 解决方法: 可能是端口被占用了,但是依旧启用成功了. 更改一下项目启用的端口号,再重新启动查看是否可以正常访问.

  4. Android Studio使用过程中Java类突然报红,但项目可运行解决方案

    1.点击File->Invalidate Caches / Restart... 2.重启Gradle,清除缓存 3.Clean Project

  5. iptables配置

    iptables -F iptables -P INPUT DROP iptables -P FORWARD DROP iptables -A INPUT -m state --state ESTAB ...

  6. JavaWeb过滤器验证登录(避免未经登录进入主页)

    今天用ssh2写了个简单的系统,发现了一个问题,我这系统必须先登录成功才能进入主页,但我在浏览器里直接输入主页地址,发现也能进入,这个肯定不好,毫无安全性可言,后经查资料发现需要登录过滤器,就试了下, ...

  7. MapReduce初学习

    内容来源,工具下载:点此链接  点此链接 Mapreduce概述: MapReduce是一种分布式计算模型,主要用于搜索领域,解决海量数据的计算问题.MR是由两个阶段组成,Map和Reduce,用户只 ...

  8. [Parcel] Running TypeScript with parcel-bundler

    TO get started with TypeScirpt quickly in your local computer is using parcel-bunlder: npm i -g parc ...

  9. 持续集成之Jenkins+Gitlab简介 [一]

    转载:http://blog.csdn.net/abcdocker/article/details/53840449 持续集成概念 持续集成Continuous Integration 持续交付Con ...

  10. 一个好用的短连接服务,mark备用

    http://to.ly/api.php? longurl=http://www.example.com 当中http://www.example.com 是你所须要转换的长链接地址.经过一个简单的g ...