分析:转自http://blog.csdn.net/mengzhengnan/article/details/47031777

一点感想:其实这个题应该是可以想到的,但是赛场上并不会

dp[i]的定义很巧妙,容斥的思路也非常清晰

然后就是讨论lucas的用法,首先成立的条件是mod是素数

但是如果这个题mod很大,组合数取模感觉就不太可做了

我认为当mod很大时,n应该很小可以预处理,但是n很大时mod应该比较小,这样也可以预处理

如果n和mod都很大我就不会了。。。。

这个题在预处理的时候,同样需要预处理逆元,如果用费马小定理的话,比较慢是O(nlogmod)的

其实有更好的O(N)筛1到mod-1的算法

详情请参考:http://blog.miskcoo.com/2014/09/linear-find-all-invert

总的来说,赛场上没做出来还是太年轻

#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e2+;
const LL mod = ;
struct Node{
LL x,y;
bool operator<(const Node &rhs)const{
return x<rhs.x;
}
}p[N];
int r,kase;
LL n,m,dp[N],f[mod+],inv[mod+];
LL C(LL n, LL m){
if(m > n) return ;
return f[n]*inv[f[m]]%mod*inv[f[n-m]]%mod;
}
LL lucas(LL n, LL m){
if(m == ) return ;
return C(n % mod, m % mod) * lucas(n / mod, m / mod) % mod;
}
int main(){
f[]=f[]=inv[]=;
for(int i=;i<mod;++i){
f[i]=f[i-]*i%mod;
inv[i]=inv[mod%i]*(mod-mod/i)%mod;
}
while(~scanf("%I64d%I64d%d",&n,&m,&r)){
bool flag=false;
for(int i=;i<=r;++i){
scanf("%I64d%I64d",&p[i].x,&p[i].y);
if(p[i].x==n&&p[i].y==m)flag=true;
}
if(flag){
printf("Case #%d: 0\n",++kase);
continue;
}
sort(p+,p++r);
memset(dp,,sizeof(dp));
++r;p[r].x=n,p[r].y=m;
for(int i=;i<=r;++i){
LL tx=p[i].x-,ty=p[i].y-,a=-,b=-;
if((tx+ty)%||(*tx-ty)%)continue;
a=(*tx-ty)/;if(a<)continue;
b=(tx+ty)/-a;if(b<)continue;
dp[i]=lucas(a+b,a);
for(int j=;j<i;++j){
if(!dp[j])continue;
if(p[i].x==p[j].x||p[i].y<=p[j].y)continue;
tx=p[i].x-p[j].x,ty=p[i].y-p[j].y,a=-,b=-;
if((tx+ty)%||(*tx-ty)%)continue;
a=(*tx-ty)/;if(a<)continue;
b=(tx+ty)/-a;if(b<)continue;
if(a==&&b==)continue;
dp[i]=(dp[i]-dp[j]*lucas(a+b,a)%mod+mod)%mod;
}
}
printf("Case #%d: %I64d\n",++kase,dp[r]);
}
return ;
}

HDU5794 A Simple Chess 容斥+lucas的更多相关文章

  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. hdu-5794 A Simple Chess(容斥+lucas+dp)

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

  3. 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)

    [题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...

  4. Codeforces Round #258 (Div. 2) 容斥+Lucas

    题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...

  5. A Simple Chess---hdu5794(容斥+Lucas)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794 题意:给你一个n*m的网格,问从(1, 1)走到(n, m)的方案数是多少,其中有r ...

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

  7. hdu_5794_A Simple Chess(lucas+dp)

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

  8. Luogu4640 BJWC2008 王之财宝 容斥、Lucas

    传送门 题意:有$N$种物品,其中$T$个物品有限定数量$B_i$,其他则没有限定.问从中取出不超过$M$个物品的方案数,对质数$P$取模.$N,M \leq 10^9 , T \leq 15 , P ...

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

随机推荐

  1. no such partition grub rescue>

    事出有因: 电脑系统是win7+ubuntu,然后在win7下把ubuntu的分区给删除了,重启,出现 no such partition grub rescue> 错误. 原因是双系统之前是由 ...

  2. POJ1118 Lining Up

    快弄死我了 最后的原因是abs和fabs的区别... 说点收获:1.cmp函数返回的是int,所以不要直接返回double相减的结果2.define inf 1e9和eps 1e-93.在整数相除得到 ...

  3. 2014-9-17二班----6 web project

    部署  加载 到 Tomcat 6.0 服务器上 web.xml           <welcome>index.jsp </welcome>   <welcome&g ...

  4. Windows下gcc以及Qt的DLL文件调用之总结(三种方法)

    DLL与LIB的区别 :1.DLL是一个完整程序,其已经经过链接,即不存在同名引用,且有导出表,与导入表lib是一个代码集(也叫函数集)他没有链接,所以lib有冗余,当两个lib相链接时地址会重新建立 ...

  5. CentOS7 升级python同时解决yum损坏问题

    CentOS7中的python版本为python2.7.5,升级到最新版的python时需要注意两个问题 新版的python安装好后要修改python的系统默认指向问题 升级到最新版python后yu ...

  6. 即时通信Spark安装和配置

    spark:Cross-platform real-time collaboration client optimized for business and organizations.Spark i ...

  7. dex

    数字交叉连接设备(Dendenkosha Electronic Exchange),就是常说的电子交换器.   数字交叉连接设备完成的主要是STM-N信号的交叉连接功能,它是一个多端口器件,它实际上相 ...

  8. Struts2的简单案例

    第一步:首先下载struts2的架包(链接地址:http://download.csdn.net/detail/ahy1024/4626028) 第二步:新建web project项目 DemoStr ...

  9. sublime-text3插件安装

    sublime-text3和sublime-text2一样安装插件前都需要先安装,Package control ,然而安装Package control的代码和sublime-text2又不相同.如 ...

  10. 任E行M1端口比特率特征码

    分辨率:800x480gps端口:com1比特率:4800设备特征码:01D1D008内存:128M