题目链接:hdu_5794_A Simple Chess

题意:

给你n,m,从(1,1)到(n,m),每次只能从左上到右下走日字路线,有k(<=100)的不能走的位置,问你有多少方案

题解:

画图可看到路线是一个杨辉三角的图,然后我们可以将对应的x,y转换到对应的点上,也可以吧杨辉三角看成一个平行四边形,

我这里看成的平行四边形,设dp[i]为从起点到第i个障碍物的的方案数,那么dp[i]=dp[i]-sum(dp[j](第j个点能走到i这个点)*(j到i的方案数))。

然后我们把终点放到最后,最后求出的dp[end]就是答案

 #include<cstdio>
#include<algorithm>
#define F(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const ll P=;
ll f[P+],r[P+];
ll C(ll n,ll m){return n<m?:f[n]*r[n-m]%P*r[m]%P;}
ll lucas(ll n,ll m){
if(n<m||m<)return ;
if(!m||n==m)return ;
return C(n%P,m%P)*lucas(n/P,m/P)%P;
}
void init(){
int i;
for(r[]=r[]=f[]=f[]=,i=;i<P;i++){
f[i]=f[i-]*i%P,r[i]=-r[P%i]*(P/i)%P;
while(r[i]<)r[i]+=P;
}
for(i=;i<P;i++)r[i]=r[i]*r[i-]%P;
} ll n,m,k,ic=,dp[]; struct point{
ll x,y;
bool operator<(const point &b)const{return x<b.x;}
}p[]; int main(){
init();
while(~scanf("%lld%lld%lld",&n,&m,&k))
{
n--,m--;
int fg=;
F(i,,k)
{
scanf("%lld%lld",&p[i].x,&p[i].y),p[i].x--,p[i].y--;
if(p[i].x==n&&p[i].y==m)fg=;
}
printf("Case #%lld: ",ic++);
if(n==&&m==)puts("");
else if((n+m)%!=||fg)puts("");
else
{
ll x,y,tx,ty;
sort(p+,p++k);
p[++k].x=n,p[k].y=m;
F(i,,k)
{
if((p[i].x+p[i].y)%==)
{
x=(p[i].x+p[i].y)/,y=min(p[i].x,p[i].y)-x;
dp[i]=lucas(x,y);
F(j,,i-)
{
if(p[j].y<p[i].y&&p[j].x<p[i].x)
{
ll xx=p[i].x-p[j].x,yy=p[i].y-p[j].y;
if((xx+yy)%==)
{
ll tx=(xx+yy)/,ty=min(xx,yy)-tx;
dp[i]-=(lucas(tx,ty)*dp[j])%P;
dp[i]=(dp[i]+P)%P;
}
}
}
}
}
printf("%lld\n",dp[k]);
}
}
return ;
}

hdu_5794_A Simple Chess(lucas+dp)的更多相关文章

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

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

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

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

  3. Codeforces Round #313 (Div. 2) E. Gerald and Giant Chess (Lucas + dp)

    题目链接:http://codeforces.com/contest/560/problem/E 给你一个n*m的网格,有k个坏点,问你从(1,1)到(n,m)不经过坏点有多少条路径. 先把这些坏点排 ...

  4. A Simple Chess (Lucas组合数 + 容斥)

    题意:走马步,要求向右向下,不能走进禁止的点.求方案数. 思路:若是n*m比较小的话,那么可以直接DP.但是这道题目不行.不过我们仔细分析可以知道从某个点到某个点是一个组合数,但是数据太大,mod值很 ...

  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+dp)

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

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

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

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

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

随机推荐

  1. CSS3入门

    CSS3 w3cschools css3  MDN英文  MDN中文 CSS3 is the latest evolution of the Cascading Style Sheets langua ...

  2. jfinal获取服务器的IP和端口

    String serverIp = getRequest().getServerName(); Integer serverPort = getRequest().getServerPort();

  3. Mybatis的失误填坑-java.lang.Integer cannot be cast to java.lang.String

    Mybatis的CRUD小Demo 为方便查看每次的增删改结果,封装了查询,用来显示数据库的记录: public static void showInfo(){ SqlSession session ...

  4. Winform 无边框窗口移动自定义边框粗细颜色

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. UITextField 设置 placeholder 的字体颜色方法

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica; color: #ff2608 } [_textField setValu ...

  6. 计算机网络课程优秀备考PPT之第三章数据链路层(三)

    为了记录自己从2016.9~2017.1的<计算机网络>助教生涯,也为了及时梳理和整写笔记! 前期博客是, 计算机网络课程优秀备考PPT之第一章概述(一) 计算机网络课程优秀备考PPT之第 ...

  7. mongodb部署单节点(一)

    部署包:mongodb-linux-x86_64-rhel55-3.0.2.tgz(百度云盘下载地址:http://pan.baidu.com/s/1jIQAGlw 密码:l7pf) 第一步:上传该文 ...

  8. linshi_temp_erweima_html

    <!doctype html><html><head><meta charset="utf-8"><meta content= ...

  9. C# 读书笔记之类与结构体

    类和结构体都包括数据和操作数据的方法 类的定义形式 class PhoneCustomer{public const string DayOfSendingBill = "Monday&qu ...

  10. webstorm常用快捷键及(idea,phpstorm,android studio通用)使用技巧

    webstorm常用快捷键 ctrl+l 格式化代码 ctrl+shift+ -/+键   折叠/展开所有代码 打开file:ctrl+shift+n 打开一个类:ctrl+n 代码提示:Ctrl+a ...