网上找了很多人的博客,都看不太懂,还是大力学长的方法好。

  要说明的一点是,因为是比较大的数字的组合数再加上mod比较小,因此用Lucas定理求组合数。

  代码如下(有注释):

 #include <stdio.h>
#include <algorithm>
#include <string.h>
#include <set>
#include <vector>
using namespace std;
const int mod = ;
typedef long long ll;
typedef pair<ll,ll> pii; int k;
ll fact[+],dp[+];
ll n,m;
vector<pii> V; void init()
{
fact[] = fact[] = ;
for(int i=;i<=;i++) fact[i] = fact[i-] * i % mod;
} void ex_gcd(ll a,ll b,ll &x,ll &y,ll &d)
{
if(!b) {d = a; x = ; y = ;}
else
{
ex_gcd(b,a%b,y,x,d);
y -= x * (a/b);
}
} ll qpow(ll a,ll b)
{
ll ans = ;
while(b)
{
if(b & ) ans = ans * a % mod;
a = a * a % mod;
b >>= ;
}
return ans;
} ll inv(ll t,ll p)
{
/*ll x,y,d;
ex_gcd(t,p,x,y,d);
return d==1 ? (x%mod+mod)%mod : -1;*/
return qpow(t,p-);
} int comb(int n,int m)
{
if(m< || n<m) return ;
return fact[n] * 1LL * inv(fact[m],mod) % mod * inv(fact[n-m],mod) % mod;
} ll Lucas(ll n,ll m,int p)
{
return m ? Lucas(n/p,m/p,p) * comb(n%p,m%p) % mod : ;
} ll solve(int i,int j) // j to i 的无视障碍的步数总数
{
if(V[i].second < V[j].second || V[i].first < V[i].first || (V[i].first+V[i].second-V[j].first-V[j].second)%) return -;
ll need = (V[i].first+V[i].second-V[j].first-V[j].second) / ;
// need 表示走过的步数
ll step = Lucas(need, V[i].first-V[j].first-need, mod);
// 第二个参数表示走过的两步的步数
// 总的步数(need)中选择若干步是走两步的
return step % mod;
} int main()
{
init();
int kase = ;
while(scanf("%I64d%I64d%d",&n,&m,&k)==)
{
V.clear();
for(int i=;i<=k;i++)
{
ll x,y;scanf("%I64d%I64d",&x,&y);
V.push_back(pii(x,y));
}
V.push_back(pii(,));
V.push_back(pii(n,m));
sort(V.begin(),V.end());
memset(dp,,sizeof(dp));
int sz = V.size();
for(int i=;i<sz;i++)
{
dp[i] = solve(i,);
if(dp[i] == -) dp[i] = ;
for(int j=;j<i;j++)
{
ll step = solve(i,j);
if(step != -)
{
dp[i] -= step * dp[j] % mod;
// 所有从原点到i这个点的路径中,从原点到禁止点再从该禁止点到i点的所有路径都是不被允许的,需要减掉
if(dp[i] < ) dp[i] = ((dp[i] % mod) + mod) % mod;
}
}
}
printf("Case #%d: %I64d\n",kase++,dp[sz-]);
}
}

HDU 5794 A Simple Chess ——(Lucas + 容斥)的更多相关文章

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

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

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

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

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

  4. HDU 5794 - A Simple Chess

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

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

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

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

  7. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  8. hdu_5794_A Simple Chess(lucas+dp)

    题目链接:hdu_5794_A Simple Chess 题意: 给你n,m,从(1,1)到(n,m),每次只能从左上到右下走日字路线,有k(<=100)的不能走的位置,问你有多少方案 题解: ...

  9. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. Java基础第四天--常用API

    常用API 基本类型包装类概述 将基本数据类型封装成对象的好处可以在对象中定义更多的功能方法操作该数据 常用的操作之一:用于基本数据类型与字符串之间的转换 基本数据类型 包装类 byte Byte s ...

  2. 实现双向绑定Proxy比defineproperty优劣如何?

    前言 双向绑定其实已经是一个老掉牙的问题了,只要涉及到MVVM框架就不得不谈的知识点,但它毕竟是Vue的三要素之一. Vue三要素 响应式: 例如如何监听数据变化,其中的实现方法就是我们提到的双向绑定 ...

  3. HTML之盒子变形动画

    4个圆形球作圆周运动 代码: <div class="box"> <div class="box1"></div> < ...

  4. python 实现服务树结构化

    1.  所有服务树数据 tree_list = [{'id': 1, 'pid': 0, 'name': '1211', 'path': '1211', 'leaf': 0, 'type': 0}, ...

  5. php--常见算法2

    <?php function zhi($number){ $f1=1; $f2=1; for($i=3;$i<=$number;$i++){ //前一个的前一个值+前一个值 $f3=$f1 ...

  6. python将list元素转为数字

    mask_x = ['11', '12', 13'] 方法1:for循环遍历 mask_x = [int(x) for x in mask_x] 方法2:map迭代 mask_x = list(map ...

  7. 记录一下Web开发环境搭建 Eclipse-Java EE 篇

    转自https://www.cnblogs.com/yangyxd/articles/5615965.html Web开发环境搭建 Eclipse-Java EE 篇 [原创内容,转载注名出处] 1. ...

  8. matlab 基础知识1

    一.数组和矩阵注意 逗号 和 分号 的区别 向量生成方式: 传统方式:行向量 :空格,逗号列向量 :分号,回车 函数方式: x = linspace(a,b,n) 等分关系,从a 到 b, n等分.n ...

  9. ogg中断处理

    ogg因为网络问题导致中断无法启动,需要重新抽取数据: --前滚抽取进程生成新的trail文件 alter extract ext147 etrollover alter ext147 extseqn ...

  10. Javaweb运行时出现Tomcat错误

    运行web项目时出现如下错误如何解决 重新配置路径,具体可参考    https://www.cnblogs.com/xueqiuxiang/p/12087947.html   中提到的配置Tomca ...