UVALive 6916---Punching Robot(卢卡斯+容斥)
题目链接
与HDU 5794相似 博客链接
题意:在一个棋盘状的网格中,有很多障碍点,求从(1,1)走到(n,m)的方法数?
思路:对障碍点进行排序,两重循环去重,加上卢卡斯定理快速求组合数;
代码如下:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <cmath>
using namespace std;
const long long mod=;
typedef long long LL;
struct Node
{
long long x;
long long y;
long long s;
}node[]; int cmp(const Node s1,const Node s2)
{
if(s1.x==s2.x)
return s1.y<s2.y;
return s1.x<s2.x;
} LL PowMod(LL a,LL b,LL MOD)
{
LL ret=;
while(b)
{
if(b&) ret=(ret*a)%MOD;
a=(a*a)%MOD;
b>>=;
}
return ret;
}
LL fac[];
LL Get_Fact(LL p)
{
fac[]=;
for(int i=; i<=p; i++)
fac[i]=(fac[i-]*i)%p;
}
LL calc(LL n,LL m,LL p)
{
LL ret=;
while(n&&m)
{
LL a=n%p,b=m%p;
if(a<b) return ;
ret=(ret*fac[a]*PowMod(fac[b]*fac[a-b]%p,p-,p))%p;
n/=p;
m/=p;
}
return ret;
} int main()
{
long long n,m;
int Case=,k,T;
Get_Fact(mod);
//cout<<calc(2,0,mod);
cin>>T;
while(T--)
{
scanf("%lld%lld%d",&n,&m,&k);
int tot=;
long long sum=;
long long x,y;
for(int i=;i<k;i++)
{
scanf("%lld%lld",&x,&y);
if(x->=&&y->=) { node[tot].x=x-; node[tot].y=y-; tot++; }
if(x->=&&y>=) { node[tot].x=x-; node[tot].y=y; tot++; }
if(x->=&&y+>=) { node[tot].x=x-; node[tot].y=y+; tot++; }
if(x>=&&y->=) { node[tot].x=x; node[tot].y=y-; tot++; }
if(x>=&&y+>=) { node[tot].x=x; node[tot].y=y+; tot++; }
if(x+>=&&y->=) { node[tot].x=x+; node[tot].y=y-; tot++; }
if(x+>=&&y>=) { node[tot].x=x+; node[tot].y=y; tot++; }
if(x+>=&&y+>=) { node[tot].x=x+; node[tot].y=y+; tot++; }
node[tot].x=x; node[tot].y=y; tot++;
}
if(tot>)
sort(node,node+tot,cmp);
//cout<<x<<y<<endl;
//for(int i=0;i<tot;i++)
//cout<<"tot: "<<node[i].x<<" "<<node[i].y<<endl;
sum=calc(n+m-,n-,mod)%mod;
// cout<<"n: "<<n<<" m: "<<m<<endl;
//cout<<tot<<endl;
//cout<<sum<<endl; for(int i=;i<tot;i++)
{
node[i].s=calc(node[i].x+node[i].y-,node[i].x-,mod)%mod;
}
for(int i=;i<tot;i++)
{
long long tt=calc(n-node[i].x+m-node[i].y,m-node[i].y,mod);
for(int j=i+;j<tot;j++)
{
if(node[j].y>=node[i].y)
{
long long d1=node[j].y-node[i].y;
long long d2=node[j].x-node[i].x;
node[j].s=(node[j].s-(node[i].s*calc(d1+d2,d1,mod))%mod)%mod;
}
}
sum=(sum-(node[i].s*tt)%mod)%mod;
sum = (sum%mod+mod)%mod;
}
printf("Case #%d: %lld\n",Case++,sum);
}
}
UVALive 6916---Punching Robot(卢卡斯+容斥)的更多相关文章
- UVALive - 6916 Punching Robot Lucas+dp
题目链接: http://acm.hust.edu.cn/vjudge/problem/96344 Punching Robot Time Limit: 1000MS64bit IO Format: ...
- UVALive 6916 Punching Robot dp
Punching Robot 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid= ...
- 【BZOJ3129】[SDOI2013]方程(容斥,拓展卢卡斯定理)
[BZOJ3129][SDOI2013]方程(容斥,拓展卢卡斯定理) 题面 BZOJ 洛谷 题解 因为答案是正整数,所先给每个位置都放一个就行了,然后\(A\)都要减一. 大于的限制和没有的区别不大, ...
- bzoj 3782 上学路线 卢卡斯定理 容斥 中国剩余定理 dp
LINK:上学路线 从(0,0)走到(n,m)每次只能向上或者向右走 有K个点不能走求方案数,对P取模. \(1\leq N,M\leq 10^10 0\leq T\leq 200\) p=10000 ...
- 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 ...
- 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 ...
- POJ1091跳蚤(容斥 + 唯一分解 + 快速幂)
题意:规定每次跳的单位 a1, a2, a3 …… , an, M,次数可以为b1, b2, b3 …… bn, bn + 1, 正好表示往左,负号表示往右, 求能否调到左边一位,即 a1* b1 ...
- HDU 4059 容斥初步练习
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...
- HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...
随机推荐
- [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action
概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在应用程序运行的时候动态创建的内容.给View添加动态内容的方式可归纳为下面几种: Inline cod ...
- JSON-fastjson
fastjson 是alibaba的一个Json处理工具包. 1.使用 JSON.toJSONString 和 JSON.parseObject fastjson只需要掌握两个静态方法:JSO ...
- Execute SQL Task 参数和变量的映射
Execute SQL Task能够执行带参数的SQL查询语句或存储过程(SP),通过SSIS的变量(Variable)对参数赋值.对于不同的Connection Manager,在Task中需要使用 ...
- MVC4做网站后台:用户管理 ——用户组补充
上次做完发现存在点问题. 1.是类型显示的为数字(如下图): 2.是不能根据类型进行查询. 首先做查询.easyui-datagrid可以添加toolbar. 在datagrid的data-optio ...
- 了解HTML锚点
概念 <a>元素 (或HTML锚元素, Anchor Element)通常用来表示一个锚点/链接.但严格来说,<a>元素不是一个链接,而是超文本锚点,可以链接到一个新文件.用i ...
- 深入理解DOM事件机制系列第四篇——事件模拟
× 目录 [1]引入 [2]模拟机制 [3]自定义事件 前面的话 事件是网页中某个特别的瞬间,经常由用户操作或通过其他浏览器功能来触发.但实际上,也可以使用javascript在任意时刻来触发特定的事 ...
- VS 2015 Enterprise第二大坑
前言 继上篇文章之后,你会继续跌进大坑,这个坑困扰我一上午,同时也会让你大跌眼镜,如果你遇到了,那么恭喜你提升自身能力和解决能力的时机到了,当然你可以通过本文继续少走不必要的弯路[我也是无意中发现的捷 ...
- 窥探Swift之数组与字典
说到数组和字典,只要是编过程的小伙伴并不陌生.在Swift中的数组与字典也有着一些让人眼前一亮的特性,今天的博客就来窥探一下Swift中的Array和Dictionary.还是沿袭之前的风格,在介绍S ...
- PJAX实现
一.浏览器历史API 浏览器历史就像一堆卡片,如下所示:
- objective-c 语法快速过(7)编译器特性ARC
ARC(是编译器特性) ARC是自iOS 5之后增加的新特性,完全消除了手动管理内存的烦琐,编译器会自动在适当的地方插入适当的retain.release.autorelease语句.你不再需要担心内 ...