bzoj2004 [Hnoi2010]Bus 公交线路 矩阵快速幂+状压DP
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=2004
题解
如果 \(N\) 没有那么大,考虑把每一位分配给每一辆车。
假设已经分配到了第 \(i\) 位,那么想要知道合不合法,我们需要知道每一辆车的上一个停靠点距离现在有多少的距离。考虑直接状压这个东西,发现它的数据量为 \(P_{p}^k\),很大。
但是我们可以发现,每一个位置到底是哪辆车无关紧要,我们只需要知道每个位置有没有车就可以了。于是数据量降低为 \(\binom p{\frac p2}\)。另外,\(0\) 这个距离是必须要选的,这样也可以剪枝掉一些。
于是大概最终的状态数为 \(130\) 左右,可以使用矩阵快速幂来加速。
时间复杂度 \(O(130^3\log n)\)。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
#define lowbit(x) ((x) & -(x))
const int N = 130;
const int NP = (1 << 10) + 7;
const int P = 30031;
int n, p, k, cnt, S, T;
int st[N], s1[NP], mp[NP];
inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
int ans = 1;
for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
return ans;
}
struct Matrix {
int a[N][N];
inline Matrix() { memset(a, 0, sizeof(a)); }
inline Matrix(const int &x) {
memset(a, 0, sizeof(a));
for (int i = 1; i <= cnt; ++i) a[i][i] = x;
}
inline Matrix operator * (const Matrix &b) {
Matrix c;
for (int k = 1; k <= cnt; ++k)
for (int i = 1; i <= cnt; ++i)
for (int j = 1; j <= cnt; ++j)
sadd(c.a[i][j], a[i][k] * b.a[k][j] % P);
return c;
}
} A, B;
inline Matrix fpow(Matrix x, int y) {
Matrix ans(1);
for (; y; y >>= 1, x = x * x) if (y & 1) ans = ans * x;
return ans;
}
inline void ycl() {
S = (1 << p) - 1;
for (int s = 0; s <= S; ++s) {
if (s) s1[s] = s1[s ^ lowbit(s)] + 1;
if (s1[s] == k && (s & 1)) st[++cnt] = s, mp[s] = cnt;
}
T = mp[(1 << k) - 1];
}
inline void ycl2() {
for (int i = 1; i <= cnt; ++i) {
int s = st[i];
if ((s >> (p - 1)) & 1) A.a[i][mp[((s << 1) & S) | 1]] = 1;
else for (int j = 0; j < p; ++j) if ((s >> j) & 1) A.a[i][mp[((s ^ (1 << j)) << 1) | 1]] = 1;
}
}
inline void work() {
ycl();
ycl2();
B.a[T][1] = 1;
B = fpow(A, n - k) * B;
printf("%d\n", B.a[T][1]);
}
inline void init() {
read(n), read(k), read(p);
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
bzoj2004 [Hnoi2010]Bus 公交线路 矩阵快速幂+状压DP的更多相关文章
- hihoCoder#1743:K-偏差排列(矩阵快速幂+状压dp)
题意 如果一个 \(1\to N\) 的排列 \(P=[P_1, P_2, ... P_N]\) 中的任意元素 \(P_i\) 都满足 \(|P_i-i| ≤ K\) ,我们就称 \(P\) 是 \( ...
- Codeforces Round #554 (Div. 2) F2. Neko Rules the Catniverse (Large Version) (矩阵快速幂 状压DP)
题意 有nnn个点,每个点只能走到编号在[1,min(n+m,1)][1,min(n+m,1)][1,min(n+m,1)]范围内的点.求路径长度恰好为kkk的简单路径(一个点最多走一次)数. 1≤n ...
- [Bzoj2004][Hnoi2010]Bus 公交线路(状压dp&&矩阵加速)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2004 看了很多大佬的博客才理解了这道题,菜到安详QAQ 在不考虑优化的情况下,先推$dp ...
- BZOJ2004: [Hnoi2010]Bus 公交线路
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2004 状压dp+矩阵乘法. f[i][s]表示从第i位至前面的i-k位,第i位必须取的状态. ...
- BZOJ2004:[HNOI2010]Bus 公交线路(状压DP,矩阵乘法)
Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1km. 作为公交车线路的规划者,小Z调查了市民的需求,决定 ...
- 【BZOJ2004】[HNOI2010]Bus 公交线路
[BZOJ2004][HNOI2010]Bus 公交线路 题面 bzoj 洛谷 题解 $N$特别大$P,K$特别小,一看就是矩阵快速幂+状压 设$f[S]$表示公交车状态为$S$的方案数 这是什么意思 ...
- 【BZOJ2004】[Hnoi2010]Bus 公交线路 状压+矩阵乘法
[BZOJ2004][Hnoi2010]Bus 公交线路 Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1 ...
- 【BZOJ】2004: [Hnoi2010]Bus 公交线路 状压DP+矩阵快速幂
[题意]n个点等距排列在长度为n-1的直线上,初始点1~k都有一辆公车,每辆公车都需要一些停靠点,每个点至多只能被一辆公车停靠,且每辆公车相邻两个停靠点的距离至多为p,所有公车最后会停在n-k+1~n ...
- 【bzoj2004】[Hnoi2010]Bus 公交线路 状压dp+矩阵乘法
题目描述 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1km. 作为公交车线路的规划者,小Z调查了市民的需求,决定按下述规则设计 ...
随机推荐
- ckeditor粘贴word图片且图片文件自动上传功能
自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...
- CentOS 7.5 通过kubeadm部署k8s-1.15.0
kubeadm是Kubernetes官方提供的用于快速安装Kubernetes集群的工具,伴随Kubernetes每个版本的发布都会同步更新,kubeadm会对集群配置方面的一些实践做调整,通过实验k ...
- js 通过浏览器直接打开应用程序(IOS,Android)
实现效果 如下图所示,在手机浏览器中访问京东的手机版网站(m.jd.com),顶部会有一个广告图,点击这个广告图,如果手机上已经安装了京东App,则直接打开,如果没有安装,则开始下载. 实现方式 1. ...
- 2017华南理工华为杯D bx回文
比赛的时候队友过了,补补题XD. 题目链接:https://scut.online/p/125(赛后补题) 125. 笔芯回文 题目描述 bx有一个长度一个字符串S,bx可以对其进行若干次操作 ...
- 【ABAP系列】SAP smartforms金额字段产生空格,除去空格的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP smartforms金额 ...
- poj1258Agri-Net(最小生成树)
题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
- PHP点滴记录
mysqli_fetch_object() //取得当前行,并作为对象返回 mysqli_fetch_row() //从结果集中取得一行,并作为枚举数组返回 mysqli_fetch_array() ...
- Java thread(2)
这一块主要是从Thread类源码的角度来分析两种线程的实现方式,这里分析的也仅仅是最基本的部分. 就从线程的启动函数 start方法开始分析 只是分析最主要的部分 在start()方法中,除了grou ...
- Java thread(1)
这一部分主要讨论 java多线程的基本相关概念以及两种java线程的实现方式: 线程与进程: 这个操作系统书上介绍得很详细,这里就列出一些比较主要的: 线程: 线程本身有很少的资源,因为所拥有的资源较 ...
- 监控服务器的脚本log_agent
监控服务器脚本: 将恶意攻击IP地址加入黑名单 1.分割日志 使用os.system 执行操作系统命令,使用重定向来分割日志 2.获取访问ip 读日志文件,获取访问ip记录,使用字符串.split来获 ...