HDU 5794 A Simple Chess (Lucas + dp)
题目链接: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)的更多相关文章
- HDU 5794 A Simple Chess Lucas定理+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 题意概述: 给出一个N*M的网格.网格上有一些点是障碍,不能经过.行走的方式是向右下角跳马步.求 ...
- HDU 5794 - A Simple Chess
HDU 5794 - A Simple Chess题意: 马(象棋)初始位置在(1,1), 现在要走到(n,m), 问有几种走法 棋盘上有r个障碍物, 该位置不能走, 并规定只能走右下方 数据范围: ...
- 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 ...
- 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 ...
- HDU 5794 A Simple Chess(杨辉三角+容斥原理+Lucas定理)
题目链接 A Simple Chess 打表发现这其实是一个杨辉三角…… 然后发现很多格子上方案数都是0 对于那写可能可以到达的点(先不考虑障碍点),我们先叫做有效的点 对于那些障碍,如果不在有效点上 ...
- hdu_5794_A Simple Chess(lucas+dp)
题目链接:hdu_5794_A Simple Chess 题意: 给你n,m,从(1,1)到(n,m),每次只能从左上到右下走日字路线,有k(<=100)的不能走的位置,问你有多少方案 题解: ...
- HDU 5794 A Simple Chess ——(Lucas + 容斥)
网上找了很多人的博客,都看不太懂,还是大力学长的方法好. 要说明的一点是,因为是比较大的数字的组合数再加上mod比较小,因此用Lucas定理求组合数. 代码如下(有注释): #include < ...
- 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)不经过坏点有多少条路径. 先把这些坏点排 ...
- A Simple Chess (Lucas组合数 + 容斥)
题意:走马步,要求向右向下,不能走进禁止的点.求方案数. 思路:若是n*m比较小的话,那么可以直接DP.但是这道题目不行.不过我们仔细分析可以知道从某个点到某个点是一个组合数,但是数据太大,mod值很 ...
随机推荐
- 51nod1055 最长等差数列
完全一脸懵逼!.dp[i][j]表示i,j为相邻的两项的最大值.两个指针两边扫的思想好劲啊这个!%%% #include<cstdio> #include<cstring> # ...
- BZOJ 2303 方格染色
首先考虑四个格子异或值为1. 然后(重点)发现每个格子的值只和最上面,最左边,和(1,1)的格子的颜色有关. 枚举(1,1)的颜色,联立方程,可以将未知数减少,那么并查集可做. 最后算答案的时候,有些 ...
- [原创] Ubuntu Linux 安装Eclipse
一 安装JDK 1.下载 JDK 7从http://www.oracle.com/technetwork/java/javasebusiness/downloads/选择下载JDK的最新版本 JDK ...
- Java程序执行过程
首先,写好Java代码,保存到硬盘中.然后在命令行中输入: javac ClassName.java 此时,这个Java类文件将编译成字节码(.class)文件.如果用Eclipse等IDE开发工具, ...
- adb shell 查找并删除文件
# -*- coding: cp936 -*- ## function: remove file ## remark: python version -- import os,sys import l ...
- Solr单机部署和集群部署
用到的相关jar包:http://pan.baidu.com/disk/home#list/path=%2Fsolr Solr目录结构 Solr 目录 Contrib :solr 为了增强自身的功能, ...
- Linux系统性能诊断工具纲要
Linux的性能分析工具众多,在微博上发现了系统性能专家Brendan D. Gregg,在最近LinuxCon NA 2014大会上发布的关于Linux性能方面的talk和幻灯片.和去年比较,今年增 ...
- 在python中如何设置当前工作目录
import osos.chdir('要设置的当前目录') >>> import os >>> os.getcwd() 'D:\\Python27' >> ...
- ArcEngine9.3报错Create output feature class failed
ArcEngine9.3执行IFeatureDataConverter.ConvertFeatureClass Method出错如下错误信息: Create output feature class ...
- window 7 下一台cp 两个mysql 配置主从
环境 : 个人 pc windows7 一台 ; 使用 : 官方下载: mysql-noinstall-5.5.11-win32.zip 1. 解压 成2个 (文件夹) mysql_master (主 ...