题目传送门

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的更多相关文章

  1. hihoCoder#1743:K-偏差排列(矩阵快速幂+状压dp)

    题意 如果一个 \(1\to N\) 的排列 \(P=[P_1, P_2, ... P_N]\) 中的任意元素 \(P_i\) 都满足 \(|P_i-i| ≤ K\) ,我们就称 \(P\) 是 \( ...

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

  3. [Bzoj2004][Hnoi2010]Bus 公交线路(状压dp&&矩阵加速)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2004 看了很多大佬的博客才理解了这道题,菜到安详QAQ 在不考虑优化的情况下,先推$dp ...

  4. BZOJ2004: [Hnoi2010]Bus 公交线路

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2004 状压dp+矩阵乘法. f[i][s]表示从第i位至前面的i-k位,第i位必须取的状态. ...

  5. BZOJ2004:[HNOI2010]Bus 公交线路(状压DP,矩阵乘法)

    Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1km. 作为公交车线路的规划者,小Z调查了市民的需求,决定 ...

  6. 【BZOJ2004】[HNOI2010]Bus 公交线路

    [BZOJ2004][HNOI2010]Bus 公交线路 题面 bzoj 洛谷 题解 $N$特别大$P,K$特别小,一看就是矩阵快速幂+状压 设$f[S]$表示公交车状态为$S$的方案数 这是什么意思 ...

  7. 【BZOJ2004】[Hnoi2010]Bus 公交线路 状压+矩阵乘法

    [BZOJ2004][Hnoi2010]Bus 公交线路 Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1 ...

  8. 【BZOJ】2004: [Hnoi2010]Bus 公交线路 状压DP+矩阵快速幂

    [题意]n个点等距排列在长度为n-1的直线上,初始点1~k都有一辆公车,每辆公车都需要一些停靠点,每个点至多只能被一辆公车停靠,且每辆公车相邻两个停靠点的距离至多为p,所有公车最后会停在n-k+1~n ...

  9. 【bzoj2004】[Hnoi2010]Bus 公交线路 状压dp+矩阵乘法

    题目描述 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1km. 作为公交车线路的规划者,小Z调查了市民的需求,决定按下述规则设计 ...

随机推荐

  1. sed进阶

    下面这些命令未必经常会用到,但当需要时,知道这些肯定是件好事. 一.多行命令 sed命令通常是对一行数据进行处理,然后下一行重复处理. sed编辑器包含了三个可用来处理多行文本的特殊命令 N:将数据流 ...

  2. Power Strings POJ - 2406

    Power Strings POJ - 2406 时限: 3000MS   内存: 65536KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 Gi ...

  3. [CSP-S模拟测试]:巨神兵(状压DP)

    题目描述 欧贝利斯克的巨神兵很喜欢有向图,有一天他找到了一张$n$个点$m$条边的有向图.欧贝利斯克认为一个没有环的有向图是优美的,请问这张图有多少个子图(即选定一个边集)是优美的?答案对$1,000 ...

  4. mui初级入门教程(三)— html5+ XMLHttpRequest 与mui ajax用法详解

    文章来源:小青年原创发布时间:2016-05-29关键词:mui,html5+,XMLHttpRequest,ajax,懒加载转载需标注本文原始地址: http://zhaomenghuan.gith ...

  5. Mac下隐藏或显示文件/文件夹

    命令行操作 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:defaults write com.apple.fi ...

  6. RESTful三理解

    目录 目录 前言 Web应用的会话状态 Cookie 资源的表现形式 HATEOAS RESTful 资源 URI 前言 最近看了一篇很赞的RESTful博客,传送门:http://www.cnblo ...

  7. 16/7/8_PHP-书写规范 PHP Coding Standard

    变量命名规范这里感觉 打算采用 匈牙利命名法+驼峰法命名,因为 PHP是弱类型语言,很多时间因为忽略了变量类型而导致犯一些低级错误.所以在前面加上类型名有助于更好的理解代码. 下载是转载 PHP书写规 ...

  8. php+form表单的文件上传

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. python实现读取excel

    实现代码如下: #读取excel,将每行数据放入一个列表,将所有列表放入一个列表形成二维列表,返回该二维列表 import xlrd class ReadExcel: def read_excel(s ...

  10. Python笔记(二十一)_内置函数、内置方法

    内置函数 issubclass(class1,class2) 判断class1类是否为class2类的子类,返回True和False 注意1:类会被认为是自身的子类 >>>issub ...