题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5794

A Simple Chess

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#### 问题描述
> 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.
#### 输入
> 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,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

题意

n*m的大棋盘,有r个障碍物,你在1,1,并且每次只能往左下走日子步,问到达(n,m)的方案数

题解

Lucas+dp.

预备:设从i点到j点过程中走横日走了x步,走竖日走了y步,则有方程2x+y==n&&x+2y==m --x=(2n-m)/3,y=(2m-n)/3。

所以方案数为C[x+y][x]。这个需要用卢卡斯处理出来。

先把所有障碍点从左上到右下排序。

dp[i]表示从1,1到(pt[i].x,pt[i].y)障碍的不经过任意其他位于它左上的障碍的情况数,则dp[i]=dp[i]-sigma(dp[j]*j到i点的所有方案数)。

代码

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define pb(v) push_back(v)
#define sz() size()
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++) typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI=acos(-1.0); //start---------------------------------------------------------------------- const int maxn=111;
const int mod=110119;
const int maxm=mod+10; LL n,m; int r;
pair<LL,LL> pt[maxn];
LL facinv[maxm],inv[maxm],fac[maxm];
LL dp[maxn]; LL get_C(LL n,LL m){
if(n<0||m<0||n<m) return 0;
return fac[n]*facinv[m]%mod*facinv[n-m]%mod;
} LL Lucas(LL n,LL m,int mod){
if(m==0) return 1LL;
return get_C(n%mod,m%mod)*Lucas(n/mod,m/mod,mod)%mod;
} LL calc(int i,int j){
LL n=pt[j].X-pt[i].X;
LL m=pt[j].Y-pt[i].Y;
if((n+m)%3) return 0;
LL sum=(n+m)/3;
if(n<0||m<0) return 0;
return Lucas(sum,n-sum,mod);
} void pre(){
fac[0]=fac[1]=1;
facinv[0]=facinv[1]=1;//facinv[0]=1!!!!
inv[1]=1;
rep(i,2,maxm){
fac[i]=fac[i-1]*i%mod;
inv[i]=(mod-mod/i)*inv[mod%i]%mod;
facinv[i]=facinv[i-1]*inv[i]%mod;
}
} int main(){
pre();
int kase=0;
while(scanf("%lld%lld%d",&n,&m,&r)==3){
clr(dp,0);
int flag=0;
rep(i,1,r+1){
scanf("%lld%lld",&pt[i].X,&pt[i].Y);
if(pt[i].X==n&&pt[i].Y==m){
flag=1;
}
}
if(flag){
printf("Case #%d: 0\n",++kase);
continue;
}
pt[0].X=1,pt[0].Y=1;
pt[r+1].X=n,pt[r+1].Y=m; sort(pt+1,pt+r+1);
dp[0]=1;
rep(i,1,r+2){
dp[i]=calc(0,i);
rep(j,1,i){
dp[i]-=dp[j]*calc(j,i)%mod;
dp[i]=(dp[i]%mod+mod)%mod;
}
}
printf("Case #%d: %lld\n",++kase,dp[r+1]);
}
return 0;
} //end-----------------------------------------------------------------------

HDU 5794 A Simple Chess dp+Lucas的更多相关文章

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

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

  2. HDU 5794 - A Simple Chess

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

  3. 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 ...

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

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

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

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

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

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

  7. HDU 5794:A Simple Chess(Lucas + DP)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794 题意:让一个棋子从(1,1)走到(n,m),要求像马一样走日字型并只能往右下角走.里 ...

  8. HDU 5794 - A Simple Nim

    题意:    n堆石子,先拿光就赢,操作分为两种:        1.任意一堆中拿走任意颗石子        2.将任意一堆分成三小堆 ( 每堆至少一颗 )        分析:    答案为每一堆的 ...

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

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

随机推荐

  1. 关于如何去Apple.cn下载Xcode以及模拟器包

    前言:对于一个懒惰的iOS开发,Xcode的更新我是迟迟没有去下载.有人或许会说:你并不是一个合格的iOS开发者! T3T 我承认自己缺少拓新精神,Apple的尿性是:坑死第一批体验者不偿命~表示本人 ...

  2. mysql 操作表结构

    整理一下对mysql表结构的简单操作,dos窗口模式的,现在基本上都是些图形化操作mysql,像这种命令形式的少了很多,暂时记忆点,以防以后忘记. 从数据库的创建开始: show databases; ...

  3. SAP Odata実行命令(2)

    前言 $ skiptokenは.アプリケーションに送信されるエントリ数を制限するために使用されます. 膨大な数のエントリが要求された場合.これはパフォーマンスの向上にも役立ちます.次のリンクがアプリケ ...

  4. 为什么我要放弃javaScript数据结构与算法(第十章)—— 排序和搜索算法

    本章将会学习最常见的排序和搜索算法,如冒泡排序.选择排序.插入排序.归并排序.快速排序和堆排序,以及顺序排序和二叉搜索算法. 第十章 排序和搜索算法 排序算法 我们会从一个最慢的开始,接着是一些性能好 ...

  5. program files与program files(x86)的区别

    简单来说:Program Files (x86)存放了一些32位的系统文件.它和正常的Program Files以及Windows文件夹一样,都属于系统文件夹,请勿随意改动. 64位Windows中提 ...

  6. Android开发——Context类的各种细节问题

    0. 前言   Context相信所有的Android开发人员基本上每天都在接触,因为它太常见了.但实际上Context有太多小的细节并不被大家所关注,那么今天我们就来学习一下那些你所不知道的细节. ...

  7. 北京Uber优步司机奖励政策(2月20日~2月21日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. 4456: [Zjoi2016]旅行者

    4456: [Zjoi2016]旅行者 https://www.lydsy.com/JudgeOnline/problem.php?id=4456 分析: 每次对当前矩阵按长边化一条分治线,然后在对分 ...

  9. JAVA面试中问及HIBERNATE与 MYBATIS的对比

    第一方面:开发速度的对比 就开发速度而言,Hibernate的真正掌握要比Mybatis来得难些.Mybatis框架相对简单很容易上手,但也相对简陋些.个人觉得要用好Mybatis还是首先要先理解好H ...

  10. quartz 核心概念

    一.quartz 核心概念 1.scheduler是一个计划调度器容器,容器里面可以盛放众多的JobDetail和trigger,当容器启动后,里面的每个JobDetail都会根据trigger按部就 ...