题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794

题意:给你一个n*m的网格,问从(1, 1)走到(n, m)的方案数是多少,其中有r个点是不可到达的;

根据公式我们可以知道每次只能走”日"型;

路径如上图所示,我们可以看到有很多点是不可达的,可达点都是满足(x+y)%3=2的;路径可以看成一个斜着放置的杨辉三角。我们只需要把坐标转换一下即可,这是没有障碍时的方案数;

让(1,1)到(n,m)中如果有一个障碍,那么我们可以用起点到终点的方法数-起点到障碍点的方法数*障碍点到终点的方法数;同样如果有 r 个,那就减去r次这样的情况;

同样处理到达每个点的时候也是这样处理的;

注意有不可达的,所以判断一下不然会re的;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
#define N 120000
#define PI 4*atan(1.0)
#define mod 110119
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL; struct node
{
LL x, y;
friend bool operator < (node p, node q)
{
if(p.x!=q.x)
return p.x < q.x;
return p.y < q.y;
}
}a[]; LL f[N] = {}; LL Pow(LL a, LL b)
{
LL ans = ;
while(b)
{
if(b&)
ans = ans*a%mod;
b/=;
a = a*a%mod;
}
return ans%mod;
} LL C(LL n, LL m)
{
if(m>n)return ;
if(m == )return ;
LL ans = f[n] * Pow(f[m], mod-)%mod * Pow(f[n-m], mod-) % mod;
return ans;
}
LL Lucas(LL n, LL m)
{
if(n< || m<)return ;///会出现不可达的情况,所以注意判断,否则会re;
if(m > n) return ;
if(m == ) return ;
return C(n%mod, m%mod) * Lucas(n/mod, m/mod) % mod;
} LL solve(LL x1, LL y1, LL x2, LL y2)
{
if((x1+y1)% != )return ;
if((x2+y2)% != )return ; LL ax = (x1+y1-)/;
LL ay = y1 - - ax; LL bx = (x2+y2-)/;
LL by = y2 - - bx; return Lucas(bx-ax, by-ay);
} int main()
{
for(int i=; i<=; i++)
f[i] = f[i-]*i % mod; LL n, m;
int t = , r;
while(scanf("%I64d %I64d %d", &n, &m, &r)!=EOF)
{
LL ans[N];///起点到i的方案数; for(int i=; i<=r; i++)
scanf("%I64d %I64d", &a[i].x, &a[i].y); sort(a+, a+r+);///按x的升序排列,再按y的升序排列; LL sum = solve(, , n, m); for(int i=; i<=r; i++)
{
ans[i] = solve(, , a[i].x, a[i].y);
for(int j=; j<i; j++)
{
ans[i] = ((ans[i] - ans[j]*solve(a[j].x, a[j].y, a[i].x, a[i].y)%mod) + mod) % mod;
}
}
for(int i=; i<=r; i++)
{
sum = (sum - ans[i]*solve(a[i].x, a[i].y, n, m)%mod + mod) % mod;
}
printf("Case #%d: %I64d\n", t++, sum);
}
return ;
}

A Simple Chess---hdu5794(容斥+Lucas)的更多相关文章

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

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

  3. hdu-5794 A Simple Chess(容斥+lucas+dp)

    题目链接: A Simple Chess Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

  4. HDU5794 A Simple Chess 容斥+lucas

    分析:转自http://blog.csdn.net/mengzhengnan/article/details/47031777 一点感想:其实这个题应该是可以想到的,但是赛场上并不会 dp[i]的定义 ...

  5. Codeforces Round #258 (Div. 2) 容斥+Lucas

    题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...

  6. hdu_5794_A Simple Chess(lucas+dp)

    题目链接:hdu_5794_A Simple Chess 题意: 给你n,m,从(1,1)到(n,m),每次只能从左上到右下走日字路线,有k(<=100)的不能走的位置,问你有多少方案 题解: ...

  7. Luogu4640 BJWC2008 王之财宝 容斥、Lucas

    传送门 题意:有$N$种物品,其中$T$个物品有限定数量$B_i$,其他则没有限定.问从中取出不超过$M$个物品的方案数,对质数$P$取模.$N,M \leq 10^9 , T \leq 15 , P ...

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

  9. 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)

    [题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...

随机推荐

  1. js精准时间迭代器(定时器)

    /** * 精准时间迭代器 * Create By Tujia @2017.05.22 * * 使用示例: * window.setMyInterval(function(){ * console.l ...

  2. 利用Python爆破数据库备份文件

    某次测试过程中,发现PHP备份功能代码如下: // 根据时间生成备份文件名 $file_name = 'D' . date('Ymd') . 'T' . date('His'); $sql_file_ ...

  3. vmware 安装 Mac OS X 10.9 Mavericks

    This guide shows how to install fresh OS X 10.9 Mavericks on VMware workstation with Windows 7 or Wi ...

  4. iOS开发-UIImageView的contentMode属性

    UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中.居右,是否缩放等,有以下几个常量可供设定:UIViewContentModeScaleToFillUIView ...

  5. RAC的搭建(二)--创建ASM磁盘

     1. 规划 表决磁盘: 1Gx3(3节点以下,建议都采用这种配置,三个磁盘加起来要大于1.8G,否则会报错) 数据磁盘: 10Gx1 闪回磁盘: 5Gx1 2. 创建共享磁盘 virtualBox上 ...

  6. [转]logging使用

    来源:https://www.cnblogs.com/nancyzhu/p/8551506.html日志 日志是跟踪软件运行时所发生的事件的一种方法.软件开发者在代码中调用日志函数,表明发生了特定的事 ...

  7. 偶值得纪念的一天-初学习C#

    今天好悲催啊,竟然生病啦,不过一切还好! 今天我们在云和数据学习的第二天,上午没有听课,似乎学习了变量的定义以及命名方法,还有变量类型的显隐式转换:我感觉这些还是在之前看书知道啦把,因此看啦看老师做的 ...

  8. 【转】java文件操作大全

    一.获得控制台用户输入的信息 public String getInputMessage() throws IOException...{         System.out.println(&qu ...

  9. Centos6下Python3的编译安装

    本文转载自 Centos6下Python3的编译安装 系统环境:CentOS 6.8-Minimal 安装Python依赖包: 1 [root@Python src]# yum install zli ...

  10. Bootstrap学习总结笔记(24)-- 基于BootstrapValidator的Form表单验证

    Form表单进行数据验证是十分必要的,我们可以自己写JS脚本或者使用JQuery Validate 插件来实现.对于Bootstrap而言,利用BootstrapValidator来做Form表单验证 ...