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. ACM--素数距离问题

    题目描述:现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度素数,则输出左侧的值及相应距离.如果输入的整数本身就是素数,则输出该素数本身,距离输出 ...

  2. <command-line>:0: error: macro names must be identifiers

    编译时的出错信息:<command-line>:0: error: macro names must be identifiers 原因: You have a -D flag with ...

  3. OpenCV 64位时 应用程序无法正常启动0x000007b 问题解决

    这问题根本不是DirectX问题,不知道网上怎么这么这样的回复.而且也不亲自验证一下.下面将自己花很多时间才解决的方式整理一下. 因为一般情况下你配置的OpenCV加入系统环境变量的都是X86下的bi ...

  4. [Git] Git把Tag推送到远程仓库

    转载: http://blog.csdn.net/hustpzb/article/details/8056518 用git tag来给工程打上标签,但是这个命令只是在本地仓库打标签而已, 为了能把标签 ...

  5. Android Design Support Library介绍之:环境搭建

    在2015年的GoogleIO大会上.具体的Material Design设计规范出炉了.全新的Android Design Support Library 格.更让人开心的是,这些很酷的风格能够通过 ...

  6. RegexHelper

    ylbtech-Unitity-cs: RegexHelper 验证帮助类 1.A,效果图返回顶部   1.B,源代码返回顶部 1.B.1,RegexMail #region RegexMail pu ...

  7. ExtJs 4中 Ext.Ajax.request提交实现waitMsg等待提示效果

    //submitForm为form表单 var myMask = new Ext.LoadMask(Ext.getBody(),{msg:"请稍等,正在导入..."}); myMa ...

  8. POJ 3077-Rounders(水题乱搞)

    Rounders Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7697   Accepted: 4984 Descript ...

  9. Yasm 1.3.0 Release Notes

    Yasm 1.3.0 Release Notes http://yasm.tortall.net/releases/Release1.3.0.html Target Audience Welcome ...

  10. windows + myeclipse 调试 linux + tomcat 的java web服务 配置方式

    一.linux tomcat配置和启动 1.catalina.sh第一行加入 declare -x CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt ...