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. Sublime text JsFormat插件的安装

    javascript格式化插件JsFormat 1.下载这插件包 https://github.com/jdc0589/JsFormat 2.点击菜单:Preferences->Browse P ...

  2. fl2440 platform总线led字符设备驱动

    首先需要知道的是,设备跟驱动是分开的.设备通过struct device来定义,也可以自己将结构体封装到自己定义的device结构体中: 例如:struct platform_device: 在inc ...

  3. http://blog.csdn.net/huang_xw/article/details/7090173

    http://blog.csdn.net/huang_xw/article/details/7090173

  4. 解决Ubuntu环境变量错误导致无法正常登录  (command 'xxx' is available in bin ls)

    一.问题产生 配置JDK时,按照搜索到的一篇文章中的做法,修改了/etc/profile文件里的内容.在原内容保持不变的基础上,大致添加了以下内容: export JAVA_HOME=.... exp ...

  5. maven命令解释

    打包:mvn package编译:mvn compile编译测试程序:mvn test-compile清空:mvn clean运行测试:mvn test生成站点目录: mvn site生成站点目录并发 ...

  6. docker入门——管理容器

    除了交互式的容器(interactive container),我们也可以创建长期运行的容器.守护式容器(daemonized container)没有交互式会话,非常适合运行应用程序和服务.大多数时 ...

  7. 集成方法:渐进梯度回归树GBRT(迭代决策树)

    http://blog.csdn.net/pipisorry/article/details/60776803 单决策树C4.5由于功能太简单.而且非常easy出现过拟合的现象.于是引申出了很多变种决 ...

  8. Charles的HTTPS抓包方法及原理,下载安装ssl/https证书

    转自:https://zhubangbang.com/charles-https-packet-capture-method-and-principle.html 本文的Charles,适应windo ...

  9. 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性——位置设置偏向

    LinearLayout有两个非常相似的属性: android:gravity与android:layout_gravity. 他们的区别在于: android:gravity 属性是对该view中内 ...

  10. 在Eclipse中导入dtd和xsd文件,使XML自动提示(转)

    DTD 类型约束文件 1. Window->Preferences->XML->XML Catalog->User Specified Entries窗口中,选择Add 按纽 ...