HDU 5794 A Simple Chess Lucas定理+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794
题意概述:
给出一个N*M的网格。网格上有一些点是障碍,不能经过。行走的方式是向右下角跳马步。求有多少种方案可以从(1,1)走到(N,M)。
多组数据,组数不超过25。N,M<=1e18。
分析:
还是水题。。。(我写这个的原因只是因为我第一次用lucas)分析一下可以发现横跳和纵跳各自的步数是确定的,所以变成了一个组合数问题。
当有障碍的时候按照第一次碰到的障碍分类,先把棋盘当成完全没有障碍,然后扣掉这些方案即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
using namespace std;
const int mo=;
typedef long long LL; LL N,M,R;
int inv[mo],J[mo],Ji[mo];
struct XY{
LL x,y;
friend bool operator < (XY a,XY b){
return a.x<b.x||a.x==b.x&&a.y<b.y;
}
friend bool operator == (XY a,XY b){
return a.x==b.x&&a.y==b.y;
}
}p[];
int f[]; int Lucas(LL x,LL y)
{
if(x<y) return ;
if(x<mo&&y<mo) return 1ll*J[x]*Ji[x-y]%mo*Ji[y]%mo;
return 1ll*Lucas(x/mo,y/mo)*Lucas(x%mo,y%mo)%mo;
}
void ready()
{
inv[]=;
for(int i=;i<mo;i++)
inv[i]=1ll*inv[mo%i]*(mo-mo/i)%mo;
J[]=,Ji[]=;
for(int i=;i<mo;i++){
J[i]=1ll*J[i-]*i%mo;
Ji[i]=1ll*Ji[i-]*inv[i]%mo;
}
}
int solve(LL n,LL m)
{
if((*n-m-)<||(*n-m-)%||(*m-n-)<||(*m-n-)%) return ;
LL a=(*m-n-)/,b=(*n-m-)/;
return Lucas(a+b,b);
}
int main()
{
ready();
int T=;
while(cin>>N>>M>>R){
int ans=solve(N,M);
if(R){
for(int i=;i<=R;i++)
cin>>p[i].x>>p[i].y;
sort(p+,p+R+);
R=unique(p+,p+R+)-p-;
memset(f,,sizeof(f));
for(int i=;i<=R;i++){
f[i]=solve(p[i].x,p[i].y);
for(int j=;j<i;j++)if(p[j].x<p[i].x&&p[j].y<p[i].y)
f[i]=(f[i]-1ll*f[j]*solve(p[i].x-p[j].x+,p[i].y-p[j].y+)%mo+mo)%mo;
}
for(int i=;i<=R;i++)
ans=(ans-1ll*f[i]*solve(N-p[i].x+,M-p[i].y+)%mo+mo)%mo;
}
cout<<"Case #"<<++T<<": "<<ans<<'\n';
}
return ;
}
HDU 5794 A Simple Chess Lucas定理+dp的更多相关文章
- HDU 5794 A Simple Chess (Lucas + dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 多校这题转化一下模型跟cf560E基本一样,可以先做cf上的这个题. 题目让你求一个棋子开始在( ...
- HDU 5794 - A Simple Chess
HDU 5794 - A Simple Chess题意: 马(象棋)初始位置在(1,1), 现在要走到(n,m), 问有几种走法 棋盘上有r个障碍物, 该位置不能走, 并规定只能走右下方 数据范围: ...
- 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 ...
- 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 ...
- HDU 5794 A Simple Chess(杨辉三角+容斥原理+Lucas定理)
题目链接 A Simple Chess 打表发现这其实是一个杨辉三角…… 然后发现很多格子上方案数都是0 对于那写可能可以到达的点(先不考虑障碍点),我们先叫做有效的点 对于那些障碍,如果不在有效点上 ...
- HDU 5794 A Simple Chess ——(Lucas + 容斥)
网上找了很多人的博客,都看不太懂,还是大力学长的方法好. 要说明的一点是,因为是比较大的数字的组合数再加上mod比较小,因此用Lucas定理求组合数. 代码如下(有注释): #include < ...
- hdu_5794_A Simple Chess(lucas+dp)
题目链接:hdu_5794_A Simple Chess 题意: 给你n,m,从(1,1)到(n,m),每次只能从左上到右下走日字路线,有k(<=100)的不能走的位置,问你有多少方案 题解: ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- HDU 5446 Unknown Treasure(Lucas定理+CRT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...
随机推荐
- o'Reill的SVG精髓(第二版)学习笔记——第六章
第六章:坐标系统变换 想要旋转.缩放或者移动图片到新的位置.可以给对应的SVG元素添加transform属性. 6.1 translate变换 可以为<use>元素使用x和y属性,以在特性 ...
- Element.getBoundingClientRect()
Element.getBoundingClientRect()方法会返回元素的大小和相对于视口的位置 语法: var domRect = element.getBoundingClientRect() ...
- javascript跳转页面
<script type="text/javascript"> function openNewTab() { parent.addExampleTab({ id: a ...
- C#实现打印
C#实现导出pdf文件,打印 using System; using System.Collections.Generic; using System.Linq; using System.Web; ...
- 随便说说Promise
为啥要说 promise ? 因为这是前端必须要掌握的一个知识,吹逼必备 首先说说 Promise 是什么? Promise 是JavaScript的第一个异步标准模型,一个包含传递信息与状态的对象, ...
- QT基于model/view数据库编程2
Qt中数据编程主要分为以下两点:1.利用qt提供类 访问数据库或者成为简单的数据库编程2.数据库编程中引入model/view编程模型 基于model/view数据库编程: qt提供model类: Q ...
- leetcode笔记(六)740. Delete and Earn
题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...
- angularjs1+requirejs+ bootstrap+ jQuery低版本配合兼容ie8+浏览器
angularjs兼容低版本IE浏览器(IE8)angularjs在1.3之后的版本都是选择放弃对IE8及更低IE版本的支持,但是就目前的开发形式来看,IE8的使用客户还是蛮多的,最近有个项目要求尽量 ...
- 揭开js之constructor属性的神秘面纱
揭开 constructor 在 Javascript 语言中,constructor 属性是专门为 function 而设计的,它存在于每一个 function 的prototype 属性中.这个 ...
- Laravel5.x 封装的上传图片类
图片缩放需要用conposer安装 ImageManagerStatic类 可参考下面的地址安装: https://www.jb51.net/article/128159.htm 控制器里: 控制器里 ...