题目链接: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. RabbitMQ之总结

    P:生成者,消息产生者: C:消息消费者: 红:消息队列: java实现 步骤: 创建连接 从连接中创建通道(相当于JDBC中的Statement) 通过channel声明(创建)队列.(如果队列存在 ...

  2. git切换分支(自记)

    git fetch git checkout feature/A4-page

  3. iOS 9: UIStackView入门

    本文转自http://www.cocoachina.com/ios/20150623/12233.html 本文由CocoaChina译者candeladiao翻译,欢迎参加我们的翻译活动.原文:iO ...

  4. Android之数据存储

    概述 1.android中包含5中数据存储方式: SharedPreferences存储数据. ContentProvider存储 文件存储 SQLlite数据库存储 网络存储 Preference  ...

  5. EXCEL数据匹配:The 'Microsoft.Jet.Oledb.4.0' provider is not registered on the local machin

    百度的处理结果: 作者:LisenYang http://blog.csdn.net/lisenyang/article/details/52106492 这篇博文里面说的,默认设置修改[启动32应用 ...

  6. Linux设备驱动剖析之IIC(一)

    写在前面 由于IIC总线只需要两根线就可以完成读写操作,而且通信协议简单,一条总线上可以挂载多个设备,因此被广泛使用.但是IIC总线有一个缺点,就是传输速率比较低.本文基于Linux-2.6.36版本 ...

  7. 【小程序+thinkphp5】 用户登陆,返回第三方session3rd

    服务器环境: centos7   php7.0 准备工作: 注册小程序,并获取 appid .appsecret 下载微信解密算法sdk : https://mp.weixin.qq.com/debu ...

  8. 【Spring Boot&&Spring Cloud系列】提高数据库访问性能

    前言 使用关系型数据库的应用系统的性能瓶颈最终还是数据库.随着业务的迅速增长,数据量会不断增大,会逐渐暴露关系型数据库的弱点,性能会大幅度的降低 项目地址:https://github.com/And ...

  9. Android 性能分析工具 TraceView

    官方地址 http://developer.android.com/tools/debugging/debugging-tracing.html 推荐:http://blog.csdn.net/inn ...

  10. Android O 获取APK文件权限 Demo案例

    1. 通过 aapt 工具查看 APK权限 C:\Users\zh>adb pull /system/priv-app/Settings . /system/priv-app/Settings/ ...