Codeforces 781D Axel and Marston in Bitland 矩阵 bitset
原文链接https://www.cnblogs.com/zhouzhendong/p/CF781D.html
题目传送门 - CF781D
题意
有一个 n 个点的图,有 m 条有向边,边有两种类型:0 和 1 。
有一个序列,其构造方案为:初始情况为 0 ;对于当前串,将当前串的 0 变成 1 ,1 变成 0 ,接到当前串的后面,得到一个新串;不断进行这个操作。
最终得到一个形如 0110100110010110…… 的无限串。
问从节点 1 出发,依次经过上述串对应的 0/1 边,最多能走多少步。
如果答案大于 $10^{18}$ ,输出 -1 。
$n\leq 500,m\leq 2n^2$
题解
首先考虑处理出两个矩阵,分别处理 0/1 两种类型的边的转移。
于是我们可以直接矩阵倍增一下得到一个 $O(n^3 \log 10^{18})$ 的做法。
再注意到我们只关系这些矩阵元素是否为 0 ,那么我们只需要用 bitset 优化一下矩阵乘法就好了。
时间复杂度 $O(n^3 \log 10^{18} / 32)$ 。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL read(){
LL x=0,f=1;
char ch=getchar();
while (!isdigit(ch)&&ch!='-')
ch=getchar();
if (ch=='-')
f=-1,ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x*f;
}
const int N=505;
const LL INF=1000000000000000000LL;
int n,m;
struct Mat{
bitset <N> v[N];
Mat(){}
Mat(int x){
for (int i=1;i<=n;i++)
v[i].reset();
for (int i=1;i<=n;i++)
v[i][i]=x;
}
friend Mat operator * (Mat a,Mat b){
Mat ans(0);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (a.v[i][j])
ans.v[i]|=b.v[j];
return ans;
}
}M[2][35];
int main(){
n=read(),m=read();
M[0][0]=M[1][0]=Mat(0);
for (int i=1;i<=m;i++){
int a=read(),b=read(),t=read();
M[t][0].v[a][b]=1;
}
int k=0;
while (M[0][k].v[1].count()&&(1LL<<(k<<1))<=INF){
k++;
M[0][k]=M[0][k-1]*M[1][k-1]*M[1][k-1]*M[0][k-1];
M[1][k]=M[1][k-1]*M[0][k-1]*M[0][k-1]*M[1][k-1];
}
LL s=0,p=0;
Mat now(0);
now.v[1][1]=1;
while (k>0&&s<=INF){
k--;
if ((now*M[p][k]).v[1].count()){
now=now*M[p][k];
s+=1LL<<(k<<1);
p^=1;
if ((now*M[p][k]).v[1].count()){
now=now*M[p][k];
s+=1LL<<(k<<1);
if ((now*M[p][k]).v[1].count()){
now=now*M[p][k];
s+=1LL<<(k<<1);
p^=1;
}
}
}
}
if (s>INF)
puts("-1");
else
cout << s << endl;
return 0;
}
Codeforces 781D Axel and Marston in Bitland 矩阵 bitset的更多相关文章
- Codeforces 781D Axel and Marston in Bitland
题目链接:http://codeforces.com/contest/781/problem/D ${F[i][j][k][0,1]}$表示是否存在从${i-->j}$的路径走了${2^{k}} ...
- CodeForces 781D Axel and Marston in Bitland DP
题意: 有一个\(n\)个点\(m\)条边的无向图,边有两种类型,分别用\(0\)和\(1\)标识 因此图中的任意一条路径都对应一个\(01\)字符串 定义一个无限长的字符串\(s\): 开始令\(s ...
- CF781D Axel and Marston in Bitland [倍增 矩阵乘法 bitset]
Axel and Marston in Bitland 好开心第一次补$F$题虽然是$Div.2$ 题意: 一个有向图,每条边是$0$或$1$,要求按如下规则构造一个序列然后走: 第一个是$0$,每次 ...
- Axel and Marston in Bitland CodeForces - 782F (bitset优化)
题目链接 $dp[0/1][i][x][y]$表示起始边为0/1, 走$2^i$ 步, 是否能从$x$走到$y$ 则有转移方程 $dp[z][i][x][y]\mid=dp[z][i-1][x][k] ...
- codeforces781D Axel and Marston in Bitland
题目链接:codeforces781D 正解:$bitset$+状压$DP$ 解题报告: 考虑用$f[t][0.1][i][j]$表示从$i$出发走了$2^t$步之后走到了$j$,且第一步是走的$0$ ...
- codeforces 711B - Chris and Magic Square(矩阵0位置填数)
题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...
- Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/678/D 简单的矩阵快速幂模版题 矩阵是这样的: #include <bits/stdc++.h&g ...
- Codeforces 917C - Pollywog(状压 dp+矩阵优化)
UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...
- Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
随机推荐
- Directory 中user Var 如何添加到通道变量中?
FS默认的配置,ACL 是 拒绝的,只能通过 Digest 的方式进行认证,一旦认证成功之后,directory 中的 var 就能在通道中通过${} 的方式获取到. 如果ACL 认证通过 ,就直接走 ...
- ebs 12.1.1升级到12.1.3
升级过程参考 Oracle电子商务套件版本12.1.3自述文件 (文档 ID 1534411.1) 应用启动到维护模式 adadmin 打以下patch 9239089 9239090 92390 ...
- mysql5.7 pxc
pxc优点总结:可以达到时时同步,无延迟现象发生完全兼容MySQL对于集群中新节点的加入,维护起来很简单数据的强一致性不足之处总结:只支持Innodb存储引擎存在多节点update更新问题,也就是写放 ...
- 1)django-建立步骤和目录说明
一:前言 django是python最流行的WEB框架. 二:django安装 pip install django 三:django项目建立步骤 1.创建django工程 django-admin ...
- Eclipse 软件 Java 解决:出现的editor does not contain a main type错误框 问题
Eclipse 软件 解决:出现的 editor does not contain a main type 错误框 问题 当你运行 Java文件是,如果弹出了下面的 错误框: 出现错误的原因: 当前的 ...
- JAVA图书管理系统汇总共27个
好多人都在搜索图书管理系统,感觉这个挺受欢迎的,所以整理了一系列的图书管理系统,让大家选择.java图书馆管理系统[优秀毕业设计论文+源码]http://down.51cto.com/data/683 ...
- Confluence 6 后台中为站点添加应用导航
Confluence 6 后台中为站点添加应用导航的连界面和方法. https://www.cwiki.us/display/CONFLUENCEWIKI/Configuring+the+Site+H ...
- http之理解304
原文:http://www.cnblogs.com/ziyunfei/archive/2012/11/17/2772729.html 如果客户端发送的是一个条件验证(Conditional Valid ...
- bs4
- python --------------网络(socket)编程
一.网络协议 客户端/服务器架构 1.硬件C/S架构(打印机) 2.软件C/S架构(互联网中处处是C/S架构):B/S架构也是C/S架构的一种,B/S是浏览器/服务器 C/S架构与socket的关系: ...