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 ...
随机推荐
- ASP.NET MVC5高级编程 之 视图
1.1理解视图约定 当创建一个项目模版时,可以注意到,项目以一种非常具体的方式包含了一个结构化的Views目录.在每一个控制器的View文件夹中,每一个操作方法都有一个同名的视图文件与其对应.这就提供 ...
- python中__init__ ,__del__ &__new__
__new__ __new__方法是用来创建对象,__new__方法需要有一个返回值,这个返回值表示创建出来的对象的引用 __init__ __init__方法在类的一个对象被建立时 ,马上执行.__ ...
- vue.js插槽
具体讲解的url https://github.com/cunzaizhuyi/vue-slot-demo //例子 用jsfiddle.net去运行就好 <!DOCTYPE html> ...
- [PHP]flock文件IO锁的使用
一.flock概述 bool flock ( resource $handle , int $operation [, int &$wouldblock ] ) 参数 handle 文 ...
- C# 我的小画板
我的画板 先看实现图 实现过程 using System; using System.Collections.Generic; using System.ComponentModel; using S ...
- js跳转页面(转)
<span id="tiao">3</span><a href="javascript:countDown"></a& ...
- (转)scikit-learn主要模块和基本使用方法
从网上看到一篇总结的很不错的sklearn使用文档,备份勿忘. 引言 对于一些开始搞机器学习算法有害怕下手的小朋友,该如何快速入门,这让人挺挣扎的.在从事数据科学的人中,最常用的工具就是R和Pytho ...
- Java测试代码(很不完整,建议大家别看,过几天会再发一次难的版本)
package ATM; import java.io.BufferedReader; import java.io.InputStreamReader; class Account{ priv ...
- Brup Suite 渗透测试笔记(七)
继续接上次笔记: 1.Burp Intruder的payload类型的子模块(Character blocks)使用一种给出的输入字符,根据指定的设置产生指定大小的字符块,表现形式为生成指定长度的字符 ...
- 动手动脑——JAVA语法基础
EnumTest.java public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Si ...