HDU 5794 A Simple Chess(杨辉三角+容斥原理+Lucas定理)
题目链接 A Simple Chess
打表发现这其实是一个杨辉三角……
然后发现很多格子上方案数都是0
对于那写可能可以到达的点(先不考虑障碍点),我们先叫做有效的点
对于那些障碍,如果不在有效点上,则自动忽略
障碍$(A, B)$如果有效,那么就要进行如下操作:
以这个点为一个新的杨辉三角的顶点,算出目标点的坐标$(x, y)$。
目标点的答案减去$C(A, B) * C(x, y)$的值。
但是这样会造成重复计算,原因是障碍之间可能有相互影响的关系。
这个时候就要考虑容斥原理,DFS消除这些重复计算即可。
计算组合数的时候可以用两种方法,
一种是快速幂
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) #define fi first
#define se second typedef long long LL; const int N = 120010;
const int A = 210;
const LL mod = 110119; struct node{
LL x, y;
friend bool operator < (const node &a, const node &b){
return (a.x + a.y) / 3 < (b.x + b.y) / 3;
}
} c[A]; int r, cnt;
LL x, y, n, m, nx, ny, ans;
LL fac[N], a[A], b[A], f[A][A]; inline LL Pow(LL a, LL b, LL Mod){ LL ret(1); for (; b; b >>= 1, (a *= a) %= Mod) if (b & 1) (ret *= a) %= Mod; return ret;}
inline LL C(LL n, LL m){ return m > n ? 0 : fac[n] * Pow(fac[m] * fac[n - m] % mod, mod - 2, mod) % mod; } LL Lucas(LL n, LL m){
if (m > n / 2) m = n - m;
return m == 0 ? 1 : C(n % mod, m % mod) % mod * (Lucas(n / mod, m / mod) % mod) % mod;
} inline LL calc(LL x, LL y){
LL n = (x + y) / 3;
LL m = y - n - 1;
return Lucas(n, m);
} inline bool check(LL x, LL y){
if (x < 0 || y < 0 || (x + y) % 3 != 2) return false;
LL n = (x + y) / 3;
if (x < n + 1 || y < n + 1) return false;
return true; } void dfs(int pre, int pos, int d, LL tmp){
if (tmp == 0LL) return;
if (d & 1) ans = (ans - tmp * b[pos] % mod) % mod;
else ans = (ans + tmp * b[pos] % mod) % mod;
rep(i, pos + 1, cnt) dfs(pos, i, d + 1, tmp * f[pos][i] % mod);
} int main(){ fac[0] = 1; rep(i, 1, N - 10) fac[i] = (fac[i - 1] * i) % mod; int ca = 0;
while (~scanf("%lld%lld%d", &n, &m, &r)){
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
memset(c, 0, sizeof c);
memset(f, 0, sizeof f);
cnt = 0;
rep(i, 1, r){
scanf("%lld%lld", &x, &y);
if (check(x, y)){
++cnt;
c[cnt].x = x;
c[cnt].y = y; }
} printf("Case #%d: ", ++ca);
if (!check(n, m)){
puts("0");
continue;
} LL x1 = (n + m) / 3, y1 = n - x1 - 1;
ans = Lucas(x1, y1);
sort(c + 1, c + cnt + 1);
rep(i, 1, cnt){
a[i] = calc(c[i].x, c[i].y);
nx = n - c[i].x + 1;
ny = m - c[i].y + 1;
if (check(nx, ny)) b[i] = calc(nx, ny); rep(j, i + 1, cnt){
nx = c[j].x - c[i].x + 1;
ny = c[j].y - c[i].y + 1;
if (check(nx, ny)) f[i][j] = calc(nx, ny);
}
} rep(i, 1, cnt) dfs(-1, i, 1, a[i]);
printf("%lld\n", (ans + mod) % mod);
} return 0;
}
另一种是扩展欧几里得。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) #define fi first
#define se second typedef long long LL; const int N = 120010;
const int A = 210;
const LL mod = 110119; struct node{
LL x, y;
friend bool operator < (const node &a, const node &b){
return (a.x + a.y) / 3 < (b.x + b.y) / 3;
}
} c[A]; int r, cnt;
LL x, y, n, m, nx, ny, ans;
LL fac[N], a[A], b[A], f[A][A]; void exgcd(LL a, LL b, LL &x, LL &y){
if (b == 0){ x = 1, y = 0; return;}
exgcd(b, a % b, x, y);
LL tmp = x; x = y; y = tmp - (a / b) * y;
} LL C(LL n, LL m){
if (m > n) return 0LL;
if (n == m) return 1LL;
LL cnt, x, y;
cnt = m;
m = fac[n];
n = fac[cnt] * fac[n - cnt] % mod;
exgcd(n, mod, x, y);
x *= m;
x %= mod;
if (x < 0) x += mod;
return x;
} LL Lucas(LL n, LL m){
if (m > n / 2) m = n - m;
if (m == 0) return 1;
return C(n % mod, m % mod) % mod * (Lucas(n / mod, m / mod) % mod) % mod;
} inline LL calc(LL x, LL y){
LL n = (x + y) / 3;
LL m = y - n - 1;
return Lucas(n, m);
} inline bool check(LL x, LL y){
if (x < 0 || y < 0 || (x + y) % 3 != 2) return false;
LL n = (x + y) / 3;
if (x < n + 1 || y < n + 1) return false;
return true; } void dfs(int pre, int pos, int d, LL tmp){
if (tmp == 0LL) return;
if (d & 1) ans = (ans - tmp * b[pos] % mod) % mod;
else ans = (ans + tmp * b[pos] % mod) % mod;
rep(i, pos + 1, cnt) dfs(pos, i, d + 1, tmp * f[pos][i] % mod);
} int main(){ fac[0] = 1; rep(i, 1, N - 10) fac[i] = (fac[i - 1] * i) % mod; int ca = 0;
while (~scanf("%lld%lld%d", &n, &m, &r)){
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
memset(c, 0, sizeof c);
memset(f, 0, sizeof f);
cnt = 0;
rep(i, 1, r){
scanf("%lld%lld", &x, &y);
if (check(x, y)){
++cnt;
c[cnt].x = x;
c[cnt].y = y; }
} printf("Case #%d: ", ++ca);
if (!check(n, m)){
puts("0");
continue;
} LL x1 = (n + m) / 3, y1 = n - x1 - 1;
ans = Lucas(x1, y1);
sort(c + 1, c + cnt + 1);
rep(i, 1, cnt){
a[i] = calc(c[i].x, c[i].y);
nx = n - c[i].x + 1;
ny = m - c[i].y + 1;
if (check(nx, ny)) b[i] = calc(nx, ny); rep(j, i + 1, cnt){
nx = c[j].x - c[i].x + 1;
ny = c[j].y - c[i].y + 1;
if (check(nx, ny)) f[i][j] = calc(nx, ny);
}
} rep(i, 1, cnt) dfs(-1, i, 1, a[i]);
printf("%lld\n", (ans + mod) % mod);
} return 0;
}
HDU 5794 A Simple Chess(杨辉三角+容斥原理+Lucas定理)的更多相关文章
- 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
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 ...
- 2014多校第六场 1007 || HDU 4927 Series 1(杨辉三角组合数)
题目链接 题意 : n个数,每操作一次就变成n-1个数,最后变成一个数,输出这个数,操作是指后一个数减前一个数得到的数写下来. 思路 : 找出几个数,算得时候先不要算出来,用式子代替,例如: 1 2 ...
- hdu 2032 一维数组实现杨辉三角
杨辉三角 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 5794 A Simple Chess (Lucas + dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 多校这题转化一下模型跟cf560E基本一样,可以先做cf上的这个题. 题目让你求一个棋子开始在( ...
- HDU 5794 A Simple Chess Lucas定理+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 题意概述: 给出一个N*M的网格.网格上有一些点是障碍,不能经过.行走的方式是向右下角跳马步.求 ...
- HDU - 5015 233 Matrix(杨辉三角/前缀+矩阵快速幂)
233 Matrix In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23 ...
- HDU 5794 A Simple Chess ——(Lucas + 容斥)
网上找了很多人的博客,都看不太懂,还是大力学长的方法好. 要说明的一点是,因为是比较大的数字的组合数再加上mod比较小,因此用Lucas定理求组合数. 代码如下(有注释): #include < ...
随机推荐
- 【linux】【指令集】查看是否打开selinux
> getenforce selinux相关原理资料参考 <鸟哥的linux私房菜> http://cn.linux.vbird.org/linux_server/0210netw ...
- python2和python3,字典和json
Python2的标准数据类型有: Numbers (数字) String (字符串) List (列表) Tuple (元组) Dictionary (字典) Python3的标准数据类型有: Num ...
- gcc——预处理(预编译),编译,汇编,链接
一,预编译 操作步骤:gcc -E hello.c -o hello.i 主要作用: 处理关于 “#” 的指令 [1]删除#define,展开所有宏定义.例#define portnumber 333 ...
- 【转载】美国人教你这样用Google
大前提:英文Google→www.google.com 第一篇 在搜索框上输入:“indexof/”inurl:lib 再按搜索你将进入许多图书馆,并且一定能下载自己喜欢的书籍. 在搜索框上输入:“i ...
- P1627 中位数
P1627 中位数 题目描述 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 输入输出格式 输入格式: 第一行为两个正整 ...
- 一个JS判断客户端是否已安装某个字体(Only IE)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- STL学习笔记2--list
List --- 双向列表 List是线性列表结构,数据查找需要一个接一个,不能直接得到元素地址,检索时间与目标元素的位置成正比.但是插入数据比较快,可以在任何位置插入数据或者删除数据.list特点是 ...
- 4.Vim编辑器与Shell命令脚本
第4章 Vim编辑器与Shell命令脚本 章节简述: 本章首先讲解如何使用Vim编辑器来编写.修改文档,然后通过逐个配置主机名称.系统网卡以及Yum软件仓库参数文件等实验,帮助读者加深Vim编辑器中诸 ...
- [git 学习篇]git管理的是修改,并非文件
你会问,什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改. 为什么说Git管理的 ...
- LaTeX Hierarchical Tables
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/52692655 简单整理一下表格中的分级 ...