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

多校这题转化一下模型跟cf560E基本一样,可以先做cf上的这个题。

题目让你求一个棋子开始在(1,1),只能像马一样走且往右下方走,不经过坏点,有多少种走法能到达(n,m)点。

比如n=6, m=5 有两个坏点,模型转换 如下图:

转换成简单模型之后,只要把棋子可能经过的坏点存到结构体中,按照x与y从小到大排序。

dp[i]表示从起点到第i个坏点且不经过其他坏点的方案数。

dp[i] = Lucas(x[i], y[i]) - sum(dp[j]*Lucas(x[i]-x[j], y[i]-x[j])) , x[j] <= x[i] && y[j] <= y[i] //到i点所有的路径数目 - 经过其他点的路径数目

那我们把最后一个点也设成坏点,比如dp[final],那么dp[final]就是答案了

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <LL, LL> P;
const int N = 1e2 + ;
const LL mod = ;
struct data {
LL x, y;
bool operator <(const data& cmp) const {
return x == cmp.x ? y < cmp.y : x < cmp.x;
}
}q[N];
LL f[mod + ]; //阶乘
dp[N]; LL Pow(LL a , LL n , LL mod) {
LL res = ;
while(n) {
if(n & )
res = res * a % mod;
a = a * a % mod;
n >>= ;
}
return res;
} LL Comb(LL a , LL b , LL mod) {
if(a < b) {
return ;
}
if(a == b) {
return ;
}
return (f[a] * Pow(f[a - b]*f[b] % mod , mod - , mod)) % mod; //乘法逆元
} LL Lucas(LL n , LL m , LL mod) {
LL ans = ;
while(m && n && ans) {
ans = (ans * Comb(n % mod , m % mod , mod)) % mod;
n /= mod;
m /= mod;
}
return ans;
} bool judge(LL x, LL y) { //判断'马' 能否走到坏点
if(x > y)
swap(x, y);
LL dec = y - x;
if(dec > x || dec* > y || (x - dec) % != || (y - dec*) % != || x - dec != y - dec*)
return true;
return false;
} P get(LL x, LL y) { //得到模型的x和y
P res;
if(x > y) {
LL dec = x - y;
res.first = dec, res.second = ;
x -= dec * , y -= dec;
res.first += x / , res.second += y / ;
} else {
LL dec = y - x;
res.first = , res.second = dec;
x -= dec, y -= dec * ;
res.first += x / , res.second += y / ;
}
return res;
} int main()
{
int Case = ;
LL n, m;
int k;
f[] = ;
for(LL i = ; i <= mod; ++i)
f[i] = f[i - ] * i % mod;
while(scanf("%lld %lld %d", &n, &m, &k) != EOF) {
n--, m--;
for(int i = ; i <= k; ++i) {
scanf("%lld %lld", &q[i].x, &q[i].y);
q[i].x--, q[i].y--;
}
printf("Case #%d: ", Case++);
if(judge(n, m)) {
printf("0\n");
continue;
}
P temp = get(n, m);
n = temp.first, m = temp.second;
int index = ;
for(int i = ; i <= k; ++i) {
if(judge(q[i].x, q[i].y))
continue;
temp = get(q[i].x, q[i].y);
if(temp.first > n || temp.second > m)
continue;
q[++index].x = temp.first, q[index].y = temp.second;
}
sort(q + , q + index + );
q[++index].x = n, q[index].y = m;
memset(dp, , sizeof(dp));
for(int i = ; i <= index; ++i) {
LL sum = ;
for(int j = ; j < i; ++j) {
if(q[i].x >= q[j].x && q[i].y >= q[j].y) {
sum = (sum + dp[j]*Lucas(q[i].x - q[j].x - q[j].y + q[i].y, q[i].y - q[j].y, mod) % mod) % mod;
}
}
dp[i] = ((Lucas(q[i].x + q[i].y, q[i].y, mod) - sum) % mod + mod) % mod;
}
printf("%lld\n", dp[index]);
}
return ;
}

HDU 5794 A Simple Chess (Lucas + dp)的更多相关文章

  1. HDU 5794 A Simple Chess Lucas定理+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 题意概述: 给出一个N*M的网格.网格上有一些点是障碍,不能经过.行走的方式是向右下角跳马步.求 ...

  2. HDU 5794 - A Simple Chess

    HDU 5794 - A Simple Chess题意: 马(象棋)初始位置在(1,1), 现在要走到(n,m), 问有几种走法 棋盘上有r个障碍物, 该位置不能走, 并规定只能走右下方 数据范围: ...

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

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

  5. HDU 5794 A Simple Chess(杨辉三角+容斥原理+Lucas定理)

    题目链接 A Simple Chess 打表发现这其实是一个杨辉三角…… 然后发现很多格子上方案数都是0 对于那写可能可以到达的点(先不考虑障碍点),我们先叫做有效的点 对于那些障碍,如果不在有效点上 ...

  6. hdu_5794_A Simple Chess(lucas+dp)

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

  7. HDU 5794 A Simple Chess ——(Lucas + 容斥)

    网上找了很多人的博客,都看不太懂,还是大力学长的方法好. 要说明的一点是,因为是比较大的数字的组合数再加上mod比较小,因此用Lucas定理求组合数. 代码如下(有注释): #include < ...

  8. Codeforces Round #313 (Div. 2) E. Gerald and Giant Chess (Lucas + dp)

    题目链接:http://codeforces.com/contest/560/problem/E 给你一个n*m的网格,有k个坏点,问你从(1,1)到(n,m)不经过坏点有多少条路径. 先把这些坏点排 ...

  9. A Simple Chess (Lucas组合数 + 容斥)

    题意:走马步,要求向右向下,不能走进禁止的点.求方案数. 思路:若是n*m比较小的话,那么可以直接DP.但是这道题目不行.不过我们仔细分析可以知道从某个点到某个点是一个组合数,但是数据太大,mod值很 ...

随机推荐

  1. 一个基于PDO的数据库操作类(新) 一个PDO事务实例

    <?php /* * 作者:胡睿 * 日期:2011/03/19 * 电邮:hooray0905@foxmail.com * * 20110319 * 常用数据库操作,如:增删改查,获取单条记录 ...

  2. 正则表达式 java版

    众所周知,在程序开发中,难免会遇到需要匹配.查找.替换.判断字符串的情况发生,而这些情况有时又比较复杂,如果用纯编码方式解决,往往会浪费程序员的时间及精力.因此,学习及使用正则表达式,便成了解决这一矛 ...

  3. 【解题报告】zju-1030 Farmland

    原题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=30 题目大意: 平面图有一些点和一条边,要求找这样的多边形: 1.边的 ...

  4. #define XXX do{...}while(0)

    <ol> <li>函数式宏定义的参数没有类型,预处理器只负责做形式上的替换,而不做参数类型检查,所以传参时要格外小心.</li> <li>调用真正函数的 ...

  5. 剑指offer-第二章排序之年龄排序

    题目:对某个公司的人的年龄(0-99)进行排序,该公司的总人数为几万人.要求时间复杂度为O(n),可以辅助O(n)的空间. 思路:实现函数为void SortAge(int ages[],int le ...

  6. jQuery基础知识--Form基础(续)

    下拉框应用 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...

  7. Kyoto Cabinet(DBM) + Kyoto Tycoon(网络层)

    项目原地址kyotocabinet: http://fallabs.com/kyotocabinet/       kyototycoon:   http://fallabs.com/kyototyc ...

  8. PHP写入Txt

    function writelog($str) { $open=fopen("log.txt","a" ); fwrite($open,$str); fclos ...

  9. ORACLE TM锁

    Oracle的TM锁类型 锁模式 锁描述 解释 SQL操作 0 none 1 NULL 空 Select 2 SS(Row-S) 行级共享锁,其他对象只能查询这些数据行 Select for upda ...

  10. 企业高并发的成熟解决方案(一)video(笔记&知识点)

    知识点 答案 什么是高可用(HA) 高并发发生在哪两处 app服务器会出现什么问题,有哪些解决方案? 数据库并发有什么要求? hadoop集群的作用 负载均衡的功能有哪些 负载均衡的分类 哪种负载均衡 ...