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 小牛之路]15 - Model Binding
Model Binding(模型绑定)是 MVC 框架根据 HTTP 请求数据创建 .NET 对象的一个过程.我们之前所有示例中传递给 Action 方法参数的对象都是在 Model Binding ...
- Eclipse安装ZooKeeper插件
Eclipse在线安装插件奇慢的解决办法 安装ZooKeeper插件步骤如下:Step 1. 在 Eclipse 菜单打开Help -> Install New Software…Step 2. ...
- 【Prince2是什么】PRINCE2认证之项目四大管理步骤
昨天谈到PRINCE2要求项目经理在做项目的时候要考虑四大核心指标(成本.时间.质量.范围)加风险与收益这两个重要要素. 然后PRINCE2基于这几大要素进行了四大管理步骤,分别是: 1.计划 2.授 ...
- Android开发学习之路-回调机制学习笔记
不知道是我学Java的时候没有认真听还是怎么的,曾经一直不知道什么是“回调”,它有什么用,百度一大堆,都太复杂看不明白(好吧是我笨),所以想把自己理解的分享给其他看到的人,大家都真正认识一下这个重要的 ...
- ElasticSearch ik分词安装
1.下载对应版本的ES ik分词 https://github.com/medcl/elasticsearch-analysis-ik/releases 2.解压elasticsearch-analy ...
- angularjs UI Libraries
angularjs UI Libraries ● ng-bootstrap is currently available. ● PrimeNG has largest number of compon ...
- HTML5入门以及新标签
HTML5 学习总结(一)--HTML5入门与新增标签 目录 一.HTML5概要 1.1.为什么需要HTML5 1.2.什么是HTML5 1.3.HTML5现状及浏览器支持 1.4.HTML5特性 ...
- LINQ系列:LINQ to SQL Exists/In/Any/All/Contains
1. Any 返回没有Product的Category var expr = from c in context.Categories where !c.Products.Any() select c ...
- (第九天)DOM事件
addEventListener 使用addEventListner()方法可以为事件目标注册事件处理程序.addEventListner()接受三个参数.第一个是要注册处理程序的事件类型,这个事件类 ...
- iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流
在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...