BZOJ 4128: Matrix (矩阵BSGS)
类比整数的做法就行了
1A爽哉
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 75;
const int sed = 137;
int n, p;
struct Matrix {
int v[MAXN][MAXN];
Matrix(){ memset(v, 0, sizeof v); }
inline void read() {
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
scanf("%d", &v[i][j]);
}
inline int hash() {
int re = 0;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
re = re * sed + v[i][j];
return re;
}
inline Matrix operator *(const Matrix &o)const {
Matrix re;
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i) if(v[i][k])
for(int j = 1; j <= n; ++j) if(o.v[k][j])
re.v[i][j] = (re.v[i][j] + v[i][k] * o.v[k][j]) % p;
return re;
}
inline bool operator ==(const Matrix &o)const {
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(v[i][j] != o.v[i][j]) return 0;
return 1;
}
}Zero, One;
inline Matrix qpow(Matrix a, int b) {
Matrix re = One;
while(b) {
if(b & 1) re = re * a;
a = a * a; b >>= 1;
}
return re;
}
map<int, int>myhash;
inline int Baby_Step_Giant_Step(Matrix a, Matrix b) {
if(b == One) return 0;
myhash.clear();
int m = int(sqrt(p)+1);
Matrix base = b;
for(int i = 0; i < m; ++i) {
myhash[base.hash()] = i;
base = a * base; //这里写a*base 和 base*a 都是一样的,因为两边同时乘以矩阵,可以乘在左边也可以乘在右边
}
Matrix tmp = One;
base = qpow(a, m);
for(int i = 1, j; i <= m+1; ++i) {
tmp = tmp * base;
if(myhash.count(j=tmp.hash()))
return i*m - myhash[j];
}
return -1;
}
inline void Pre_Work() {
for(int i = 1; i <= n; One.v[i][i] = 1, ++i);
}
int main() {
scanf("%d%d", &n, &p);
Pre_Work();
Matrix A, B;
A.read(); B.read();
printf("%d\n", Baby_Step_Giant_Step(A, B));
}
BZOJ 4128: Matrix (矩阵BSGS)的更多相关文章
- BZOJ 4128: Matrix
BZOJ 4128: Matrix 标签(空格分隔): OI BZOJ 大步小步 矩阵 费马小定理 Time Limit: 10 Sec Memory Limit: 128 MB Descriptio ...
- bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希
题目 给定矩阵A, B和模数p,求最小的正整数x满足 A^x = B(mod p). 分析 与整数的离散对数类似,只不过普通乘法换乘了矩阵乘法. 由于矩阵的求逆麻烦,使用 $A^{km-t} = B( ...
- BZOJ 4128 Matrix BSGS+矩阵求逆
题意:链接 方法: BSGS+矩阵求逆 解析: 这题就是把Ax=B(mod C)的A和B换成了矩阵. 然而别的地方并没有修改. 所以就涉及到矩阵的逆元这个问题. 矩阵的逆元怎么求呢? 先在原矩阵后接一 ...
- BZOJ 4128 Matrix ——BSGS
矩阵的BSGS. 只需要哈希一下存起来就可以了. 也并不需要求逆. #include <map> #include <cmath> #include <cstdio> ...
- bzoj4128 Matrix 矩阵 BSGS
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4128 题解 想了十分钟没有任何思路. 然后一眼瞥见一句话"数据保证在 \(p\) 内 ...
- 【CSS3】 理解CSS3 transform中的Matrix(矩阵)
理解CSS3 transform中的Matrix(矩阵) by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu ...
- 理解CSS3 transform中的Matrix(矩阵)
一.哥,我被你吓住了 打架的时候会被块头大的吓住,学习的时候会被奇怪名字吓住(如“拉普拉斯不等式”).这与情感化设计本质一致:界面设计好会让人觉得这个软件好用! 所以,当看到上面“Matrix(矩阵) ...
- HDU5015 233 Matrix(矩阵高速幂)
HDU5015 233 Matrix(矩阵高速幂) 题目链接 题目大意: 给出n∗m矩阵,给出第一行a01, a02, a03 ...a0m (各自是233, 2333, 23333...), 再给定 ...
- 理解CSS3 transform中的Matrix(矩阵)——张鑫旭
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2427 一.哥,我被你 ...
随机推荐
- linux下对服务器性能监控shell脚本
#!/bin/bash #提取本服务器的IP地址信息 ENO1=`ifconfig | sed -n '1,1p' | awk -F ' ' '{print $1}'` IP=` -d -d &quo ...
- 安装echo框架
视频地址: https://www.bilibili.com/video/av63492462?p=31 echo文档地址: https://echo.labstack.com/guide/insta ...
- Python学习7——异常
编写程序时,通常能够区分正常和异常情况.为了处理这些异常,可在每个可能出现异常的地方都使用上条件语句,但这样大大降低了程序的可读性,那么怎么解决哪?Python提供强大的替代解决方案——异常处理机制. ...
- Windows 安装和配置 WSL
Windows 安装和配置 WSL 什么是 WSL 引用百度百科的一段话: Windows Subsystem for Linux(简称WSL)是一个为在Windows 10上能够原生运行Linux二 ...
- hdu 5651 重复全排列+逆元
知识点: n个元素,其中a1,a2,····,an互不相同,进行全排列,可得n!个不同的排列. 若其中某一元素ai重复了ni次,全排列出来必有重复元素,其中真正不同的排列数应为 ,即其重复度为ni! ...
- 10-Perl 循环
1.Perl 循环一般情况下,语句是按顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推.有的时候,可能需要多次执行同一块代码.编程语言提供了更为复杂执行路径的多种控制结构.循环语句允许 ...
- imx8移植opencv(3.0以上版本)笔记
基本步骤参考我同事的博客:https://blog.csdn.net/hunzhangzui9837/article/details/89846928 以下是在移植到imx8平台时的笔记和遇到的问题及 ...
- squoosh
谷歌在线压缩图片
- shiro学习(四、shiro集成spring+springmvc)
依赖:spring-context,spring-MVC,shiro-core,shiro-spring,shiro-web 实话实说:web.xml,spring,springmvc配置文件好难 大 ...
- redis的数据结构及操作命令
一.字符串: redis中最为基础的存储类型,以二进制存储,value的字符串最多512M,Key做多1024字节. 常用命令:赋值(set).取值(get).删除(del),递增(incr/incr ...