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 ...
随机推荐
- Git fetch和git pull的区别(转)
原文: http://www.tech126.com/git-fetch-pull/ Git中从远程的分支获取最新的版本到本地有这样2个命令:1. git fetch:相当于是从远程获取最新版本到本地 ...
- QAQ数论模板笔记√
#include <cstdio> using namespace std; long long gcd(long long a, long long b) { // (a, b) ret ...
- lintcode 中等题:Majority number II 主元素 II
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...
- 32. Longest Valid Parentheses
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- Intellij IDEA新建一个EJB工程(二)
从博文:http://www.cnblogs.com/yangyquin/p/5328344.html 中可以知道如何利用Intellij IDEA建立一个EJB Module,还有新建一个测试Mod ...
- getpeername
定义: int getpeername(int s, struct sockaddr *name, socklen_t *namelen); 描述: 获取socket的对方地址 得到对方的地址 s ...
- 修改tabbarcontroller选中图片及选中颜色
1.修改选中图片: UITabBarItem* item = [self.tabBarController.tabBar.items objectAtIndex:1]; //从0开始 item.s ...
- .net 生成拼音码与五笔码
首先加入配置文件: <?xml version="1.0" encoding="utf-8" ?> <CodeConfig> <S ...
- 运行Android应用时提示ADB是否存在于指定路径问题
打开eclipse,选择指定的Android应用工程并Run,提示: [2014-06-28 11:32:26 - LinearLayout] The connectionto adb is down ...
- Android Activity形象描述
Activity就是形象的说就是一个容器,在里面放置各种控件(按钮,文本,复选框等),就形成了软件的界面~ Activity是可见的,如果不加任何控件的话,那么就像Windows中的空白窗体一样 通过 ...