HDU5794 A Simple Chess 容斥+lucas
分析:转自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的更多相关文章
- 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 ...
- hdu-5794 A Simple Chess(容斥+lucas+dp)
题目链接: A Simple Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)
[题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...
- Codeforces Round #258 (Div. 2) 容斥+Lucas
题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...
- A Simple Chess---hdu5794(容斥+Lucas)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794 题意:给你一个n*m的网格,问从(1, 1)走到(n, m)的方案数是多少,其中有r ...
- 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+dp)
题目链接:hdu_5794_A Simple Chess 题意: 给你n,m,从(1,1)到(n,m),每次只能从左上到右下走日字路线,有k(<=100)的不能走的位置,问你有多少方案 题解: ...
- Luogu4640 BJWC2008 王之财宝 容斥、Lucas
传送门 题意:有$N$种物品,其中$T$个物品有限定数量$B_i$,其他则没有限定.问从中取出不超过$M$个物品的方案数,对质数$P$取模.$N,M \leq 10^9 , T \leq 15 , P ...
- 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 ...
随机推荐
- 李洪强漫谈iOS开发[C语言-009] - C语言关键字
// // main.m // 04 - C语言关键字 // // Created by vic fan on 16/7/12. // Copyright © 2016年 李洪强. All r ...
- leetcode 4 : Median of Two Sorted Arrays 找出两个数组的中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- *[hackerrank]Consecutive Subsequences
https://www.hackerrank.com/contests/w6/challenges/consecutive-subsequences 求数组中被k整除的子段和有几个.这个要利用sum[ ...
- *[codility]MissingInteger
今天开始刷刷codility上respectable的题目,难度适中. https://codility.com/demo/take-sample-test/missing_integer 本题是找出 ...
- ibaits与spring整合的心得
Ibatis2.3与spring3.0整合,其要明确一下,Ibatis与Hibernate一样都是dao层链接数据库用的框架.它是一个轻量级的orm框架,比Hibernate更加灵活. sqlMapC ...
- 分享一个免费SSL证书申请网站,给网站开启https协议 | 张戈博客
这些天,由于公司的业务需求,接触到了ssl证书和https协议.博客前几篇文章也分享了在WEB服务器上安装SSL证书,为网站开启https协议的教程,感兴趣的童鞋可以前往查看相关文章: <Lin ...
- SQL 分组排序分页(大神帮写的膜拜一下)
查询全部: SELECT P3.ID, P3.Name, P3.AddTimeFROM (SELECT Name, MAX(AddTime) AS MaxAddTime FROM Product AS ...
- 【C#设计模式——创建型模式】简单工场模式
进入码农行列也有一年半载了,仍然感觉自己混混沌沌,无所事事,无所作为,,,想想都下气,下气归下气,仍要奋起潜行,像愤怒的小鸟一边又一遍的冲向猪头也好,像蜗牛一样往前蹭也罢,总之要有蚂蚁啃骨头的精神!! ...
- 客户视角:Oracle ETL工具ODI
客户视角:Oracle ETL工具ODI 数据集成已成为企业在追求市场份额中的关键技术组件,与依靠手工编码的方式不同,越来越多的企业选择完整的数据集成解决方案来支持其IT战略,从大数据分析到云平台的集 ...
- Fragment 和 FragmentActivity的使用(二)
今天继续完成剩下的学习部分,现在项目很多地方使用viewpager来提供滑动,今天记录学习viewpager配合fragment的显示,增加一个CallLogsFragment配合之前SMSLis ...