题目链接:

A Simple Chess

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 65536/65536 K (Java/Others)

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
 
 
题意:
 
走日字从(1,1)到(n,m)且不经过障碍的方案数;
 
思路:
 
原来向下和向右移动的方案数是C(n+m,m),这个是先把日字变成原来熟悉的走法,可以画个图研究一下,最后发现是(0,0)到(2*fy-fx/3,2*fx-fy/3)的方案数
不经过障碍可以用容斥加dp解决,dp[i]表示从起点到达第i个点中间不经过障碍点的方案数,那么dp[i]=起点到达i的总方案数-∑dp[j]*(j点到达i点的总方案数)
还有就是要预处理出阶乘,同时n和m都太大要用lucas定理化简,C(n,m)%mod=C(n/mod,m/mod)*C(n%mod,m%mod)%mod;
 
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod=110119;
const int maxn=110;
LL n,m,x[maxn],y[maxn],dp[maxn],p[110130];
int r;
inline void init()
{
p[0]=1;
for(int i=1;i<=110119;i++)p[i]=p[i-1]*(LL)i%mod;
}
LL pow_mod(LL a,LL b)
{
LL s=1,base=a;
while(b)
{
if(b&1)s=s*base%mod;
base=base*base%mod;
b>>=1;
}
return s;
}
LL cal(LL a,LL b)
{
if(a<mod&&b<mod)
{
if(b>a)return 0;
return p[a]*pow_mod(p[b],mod-2)%mod*pow_mod(p[a-b],mod-2)%mod;
}
return cal(a/mod,b/mod)*cal(a%mod,b%mod)%mod;
}
LL solve(int L,int R)
{
LL fx=x[R]-x[L],fy=y[R]-y[L];
if((2*fy-fx)%3||(2*fx-fy)%3||2*fy<fx||2*fx<fy)return 0;
LL up=(2*fy-fx)/3,down=(fx+fy)/3;
return cal(down,up);
}
int main()
{
init();
int Case=0;
while(scanf("%lld%lld%d",&n,&m,&r)!=EOF)
{
memset(dp,0,sizeof(dp));
int flag=0;
x[0]=1,y[0]=1;
for(int i=1;i<=r;i++)
{
scanf("%lld%lld",&x[i],&y[i]);
if(x[i]==n&&y[i]==m)flag=1;
}
LL ans=0;
if(!flag)
{
x[0]=1,y[0]=1;
dp[0]=1;
x[++r]=n,y[r]=m;
for(int i=1;i<=r;i++)
{
for(int j=1;j<=i;j++)
{
if(x[j]>=x[i]&&y[j]>=y[i])swap(x[i],x[j]),swap(y[i],y[j]);
}
}
for(int i=1;i<=r;i++)dp[i]=solve(0,i);
for(int i=1;i<=r;i++)
{
for(int j=1;j<i;j++)
{
if(x[j]<=x[i]&&y[j]<=y[i])dp[i]=(dp[i]-dp[j]*solve(j,i)%mod+mod)%mod;
}
}
for(int i=1;i<=r;i++)if(x[i]==n&&y[i]==m)ans=dp[i];
}
printf("Case #%d: %lld\n",++Case,ans);
}
return 0;
}

  

 

hdu-5794 A Simple Chess(容斥+lucas+dp)的更多相关文章

  1. 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)的路径条数。

    A Simple Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  2. HDU5794 A Simple Chess 容斥+lucas

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

  3. HDU 5794 - A Simple Chess

    HDU 5794 - A Simple Chess题意: 马(象棋)初始位置在(1,1), 现在要走到(n,m), 问有几种走法 棋盘上有r个障碍物, 该位置不能走, 并规定只能走右下方 数据范围: ...

  4. HDU 5794 A Simple Chess dp+Lucas

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5794 A Simple Chess Time Limit: 2000/1000 MS (Java/O ...

  5. HDU 5794 A Simple Chess (容斥+DP+Lucas)

    A Simple Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5794 Description There is a n×m board ...

  6. HDU 5794 A Simple Chess(杨辉三角+容斥原理+Lucas定理)

    题目链接 A Simple Chess 打表发现这其实是一个杨辉三角…… 然后发现很多格子上方案数都是0 对于那写可能可以到达的点(先不考虑障碍点),我们先叫做有效的点 对于那些障碍,如果不在有效点上 ...

  7. HDU 5794 A Simple Chess ——(Lucas + 容斥)

    网上找了很多人的博客,都看不太懂,还是大力学长的方法好. 要说明的一点是,因为是比较大的数字的组合数再加上mod比较小,因此用Lucas定理求组合数. 代码如下(有注释): #include < ...

  8. HDU 5794 A Simple Chess (Lucas + dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 多校这题转化一下模型跟cf560E基本一样,可以先做cf上的这个题. 题目让你求一个棋子开始在( ...

  9. HDU 5794 A Simple Chess Lucas定理+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 题意概述: 给出一个N*M的网格.网格上有一些点是障碍,不能经过.行走的方式是向右下角跳马步.求 ...

随机推荐

  1. win8.1和centos6.5 双系统启动问题

    笔记本系统为centos 6.5,由grub引导启动,安装了win 8.1后,开机直接进入win 8.1,没有出现centos6.5 引导项,解决办法: 一.开机按ESC键进入启动顺序菜单,选择cen ...

  2. Linux 查公网出口IP

    wget http://members.3322.org/dyndns/getipcat getip

  3. (repost)在ARM Linux内核中增加一个新的系统调用

    实验平台内核版本为4.0-rc1,增加一仅仅打印Hello World的syscall,最后我们在用户空间swi验证 实验平台内核版本为4.0-rc1,增加的系统调用仅仅是简单打印一个Hello Wo ...

  4. Eclipse Bug: Unhandled event loop exception No more handles

    我的解决方法如下: I had the same problem, turned out that TeamViewer was causing this. In your TeamViewer go ...

  5. iOS 10 / Swift 3.0 / XCode 8 总结

    1,iOS10 新增的privacy settings iOS10添加了新的权限控制范围 如果你尝试访问这些隐私数据时得到如下错误: > This app has crashed because ...

  6. 打印机服务器搭建 -cups

    系统:ubuntu 14.04 http://foo2zjs.rkkda.com/INSTALL 安装cups   sudo apt-get install cups 安装完成后,把打印机的数据线与服 ...

  7. appium元素定位及操作

    1.测试用例准备 数据准备   前提条件   操作步骤    预期结果 2.TestNG 用例组织:@Test @Before Class 结果验证:Assert 数据驱动:@DataProvide ...

  8. synchronized细节问题

    一.synchronized有锁重入的特点,某个线程得到对象的锁后,再次请求此对象可以再次得到改对象的锁.如下示例,在method1中调用method2,在method2中调用method3,而met ...

  9. Ubuntu 软件 安装 下载 及更新

    1  软件安装 sudo apt-get install 2 软件搜索 sudo   apt-cache search 3  系统已经安装了什么软件 dpkg  -l 是否确切安装了某软件 dpkg ...

  10. android 调用.NET WebServices

    下载Ksoap2.jar, import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.*;import org.ksoap2.tra ...