题目链接

BZOJ4000

题解

注意题目中的编号均从\(0\)开始= =

\(m\)特别小,考虑状压

设\(f[i][s]\)为第\(i\)行为\(s\)的方案数

每个棋子能攻击的只有本行,上一行,下一行,

我们能迅速找出哪些状态是合法的,以及每个状态所对应的上一行攻击位置的并和下一行攻击位置的并

如果两个状态上下相互攻击不到,就是合法的转移

我们弄一个\(2^m * 2^m\)的转移矩阵,就可以矩阵优化了

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define uint unsigned int
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 65,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int s1,s2,s3,n,m,p,k;
struct Matrix{
uint s[maxn][maxn]; int n,m;
Matrix(){memset(s,0,sizeof(s)); n = m = 0;}
}A,F,Fn;
inline Matrix operator *(const Matrix& a,const Matrix& b){
Matrix c;
if (a.m != b.n) return c;
c.n = a.n; c.m = b.m;
for (int i = 0; i < c.n; i++)
for (int j = 0; j < c.m; j++)
for (int k = 0; k < a.m; k++)
c.s[i][j] += a.s[i][k] * b.s[k][j];
return c;
}
inline Matrix qpow(Matrix a,int b){
Matrix ans; ans.n = ans.m = a.n;
for (int i = 0; i < ans.n; i++) ans.s[i][i] = 1;
for (; b; b >>= 1,a = a * a)
if (b & 1) ans = ans * a;
return ans;
}
int f[maxn];
bool check(int s){
for (int i = 0; i < m; i++){
if (s & (1 << i)){
if (i + 1 >= p - k){
if (s & (s2 << (i + 1 - (p - k)))) return false;
}
else if (s & (s2 >> ((p - k) - i - 1))) return false;
}
}
return true;
}
int getu(int s){
int re = 0;
for (int i = 0; i < m; i++){
if (s & (1 << i)){
if (i + 1 >= p - k) re |= s1 << (i + 1 - (p - k));
else re |= s1 >> ((p - k) - i - 1);
}
}
return re;
}
int getd(int s){
int re = 0;
for (int i = 0; i < m; i++){
if (s & (1 << i)){
if (i + 1 >= p - k) re |= s3 << (i + 1 - (p - k));
else re |= s3 >> ((p - k) - i - 1);
}
}
return re;
}
void print(int x){
for (int i = 4; i >= 0; i--)
printf("%d",(x & (1 << i)) != 0);
}
int main(){
n = read(); m = read();
p = read(); k = read();
REP(i,p) s1 = (s1 << 1) + read();
REP(i,p){
if (i == k + 1) s2 <<= 1,read();
else s2 = (s2 << 1) + read();
}
REP(i,p) s3 = (s3 << 1) + read();
int N = 1 << m;
F.n = N; F.m = 1;
for (int s = 0; s < N; s++)
if (check(s)) F.s[s][0] = 1;
A.n = A.m = N;
for (int s = 0; s < N; s++){
if (!check(s)) continue;
for (int e = 0; e < N; e++){
if (!check(e)) continue;
int u = getu(s),d = getd(e);
if (!(s & d) && !(e & u))
A.s[s][e] = 1;
}
}
Fn = qpow(A,n - 1) * F;
uint ans = 0;
for (int i = 0; i < N; i++) ans += Fn.s[i][0];
cout << ans << endl;
return 0;
}

BZOJ4000 [TJOI2015]棋盘 【状压dp + 矩阵优化】的更多相关文章

  1. [BZOJ4000][TJOI2015]棋盘(状压DP+矩阵快速幂)

    题意极其有毒,注意给的行列都是从0开始的. 状压DP,f[i][S]表示第i行状态为S的方案数,枚举上一行的状态转移.$O(n2^{2m})$ 使用矩阵加速,先构造矩阵a[S1][S2]表示上一行为S ...

  2. BZOJ 4000: [TJOI2015]棋盘( 状压dp + 矩阵快速幂 )

    状压dp, 然后转移都是一样的, 矩阵乘法+快速幂就行啦. O(logN*2^(3m)) ------------------------------------------------------- ...

  3. Codeforces 917C - Pollywog(状压 dp+矩阵优化)

    UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...

  4. 【BZOJ4000】【LOJ2104】【TJOI2015】棋盘 (状压dp + 矩阵快速幂)

    Description ​ 有一个\(~n~\)行\(~m~\)列的棋盘,棋盘上可以放很多棋子,每个棋子的攻击范围有\(~3~\)行\(~p~\)列.用一个\(~3 \times p~\)的矩阵给出了 ...

  5. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

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

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

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

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

  8. 【bzoj1097】[POI2007]旅游景点atr 状压dp+堆优化Dijkstra

    题目描述 FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺序不是完全随意的,比如说FGD不希望在刚吃过一顿大餐之后立刻去下一个 ...

  9. 【XSY2524】唯一神 状压DP 矩阵快速幂 FFT

    题目大意 给你一个网格,每个格子有概率是\(1\)或是\(0\).告诉你每个点是\(0\)的概率,求\(1\)的连通块个数\(\bmod d=0\)的概率. 最开始所有格子的概率相等.有\(q\)次修 ...

随机推荐

  1. 1.VS Code 开发C#入门 安装Dotnet core

    1. dot.net  网站 下载 .NET Core 1.0  (https://www.microsoft.com/net/download/core) 2. 打开命名提示符: 3.dotnet ...

  2. PageHelper 记录总条数不正确问题处理

    //PageHelper.startPage会返回一个page对象,这个对象在查询结果出来后会把页数,记录总数给page对象,用page.getPages()和getTotal()获取页数和记录总数. ...

  3. Struts2 In Action笔记_页面到动作的数据流入和流出

    因为回答百度知道的一个问题,仔细查看了<Struts2 In Action>,深入细致的看了 “数据转移OGNL 和 构建视图-标签”,很多东西才恍然大悟. 一直觉得国外写的书很浮,不具有 ...

  4. js转换时间戳成日期格式

    <script> function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().rep ...

  5. Java - 类加载的时候,是有缺省同步锁的

    类加载的时候,是有缺省同步锁的

  6. Python 文件读写 文件和路径

    1.在Windows上,使用倒斜杆作为文件夹之间的分隔符,在Linux上,使用正斜杠作为路径分隔符.在编写Python脚本时,可以os.path.join()函数来处理 在Windows环境下命令如下 ...

  7. Dungeon Master(逃脱大师)-BFS

    Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! Th ...

  8. Entrez Direct

    安装 cd ~/bin/bashperl -MNet::FTP -e \'$ftp = new Net::FTP("ftp.ncbi.nlm.nih.gov", Passive = ...

  9. django之模型层

    1. ORM MVC或者MTV框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员 ...

  10. DiyCode开源项目 TopicActivity 分析

    1.首先看看TopActivity效果.    2.TopicActivity是一个继承BaseActivity的.前面分析过BaseActivity了.主要有一个标题栏,有返回的图标. 3.贴一下T ...