题目链接

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4928

与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(卢卡斯+容斥)的更多相关文章

  1. UVALive - 6916 Punching Robot Lucas+dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/96344 Punching Robot Time Limit: 1000MS64bit IO Format: ...

  2. UVALive 6916 Punching Robot dp

    Punching Robot 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid= ...

  3. 【BZOJ3129】[SDOI2013]方程(容斥,拓展卢卡斯定理)

    [BZOJ3129][SDOI2013]方程(容斥,拓展卢卡斯定理) 题面 BZOJ 洛谷 题解 因为答案是正整数,所先给每个位置都放一个就行了,然后\(A\)都要减一. 大于的限制和没有的区别不大, ...

  4. bzoj 3782 上学路线 卢卡斯定理 容斥 中国剩余定理 dp

    LINK:上学路线 从(0,0)走到(n,m)每次只能向上或者向右走 有K个点不能走求方案数,对P取模. \(1\leq N,M\leq 10^10 0\leq T\leq 200\) p=10000 ...

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

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

  7. POJ1091跳蚤(容斥 + 唯一分解 + 快速幂)

      题意:规定每次跳的单位 a1, a2, a3 …… , an, M,次数可以为b1, b2, b3 …… bn, bn + 1, 正好表示往左,负号表示往右, 求能否调到左边一位,即 a1* b1 ...

  8. HDU 4059 容斥初步练习

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

  9. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

随机推荐

  1. 《3D Math Primer for Graphics and Game Development》读书笔记2

    <3D Math Primer for Graphics and Game Development>读书笔记2 上一篇得到了"矩阵等价于变换后的基向量"这一结论. 本篇 ...

  2. spring快速入门(四)

    一.在spring快速入门(三)的基础上,我们来了解BeanFactory及配置. Client package com.murong.client; import org.springframewo ...

  3. Java中常见的29个运行异常

    ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, CannotR ...

  4. Android 解决方法数 65536 (65k) 限制

    可能出现的错误信息: Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: ...

  5. 《Qt Quick 4小时入门》学习笔记3

    http://edu.csdn.net/course/detail/1042/14807?auto_start=1 Qt Quick 4小时入门 第八章:Qt Quick中的锚(anchors)布局 ...

  6. SQLSERVER中的假脱机spool

    SQLSERVER中的假脱机spool 我发现网上对于假脱机的解释都非常零散,究竟假脱机是什么? 这几天在家里研究了一下,收集了很多网上的资料 假脱机是中文的翻译,而英文的名字叫做 spool 在徐老 ...

  7. UGUI 之获取当前控件的高度

    当Canvas Scaler选择Constant Pixel Size 当前的分辨率会被被固定,可以用RectTransform类里面的.rect变量值获取 height或Width. 在次情况下获取 ...

  8. UGUI 之Scroll Rect 坑

    由于UGUI没有提供类似Scroll View类似的控件,但提供了ScrollRect主机.可以自定义Scroll View 同时提供了Mask组件来遮罩超出Scroll Rect对象的范围, 之所以 ...

  9. OpenCASCADE PCurve of Topological Face

    OpenCASCADE PCurve of Topological Face eryar@163.com Abstract. OpenCASCADE provides a class BRepBuil ...

  10. Java设计模式之建造者模式(Builder)

    前言: 最近一直在学习okHttp,也对其做了一些整理,okHttp和Retrofit结合大大加速我们的开发效率,源码里面采用了很多设计模式,今天我们来学习一下其中的设计模式之一建造者模式. 建造者模 ...