类比整数的做法就行了

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

  1. BZOJ 4128: Matrix

    BZOJ 4128: Matrix 标签(空格分隔): OI BZOJ 大步小步 矩阵 费马小定理 Time Limit: 10 Sec Memory Limit: 128 MB Descriptio ...

  2. bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希

    题目 给定矩阵A, B和模数p,求最小的正整数x满足 A^x = B(mod p). 分析 与整数的离散对数类似,只不过普通乘法换乘了矩阵乘法. 由于矩阵的求逆麻烦,使用 $A^{km-t} = B( ...

  3. BZOJ 4128 Matrix BSGS+矩阵求逆

    题意:链接 方法: BSGS+矩阵求逆 解析: 这题就是把Ax=B(mod C)的A和B换成了矩阵. 然而别的地方并没有修改. 所以就涉及到矩阵的逆元这个问题. 矩阵的逆元怎么求呢? 先在原矩阵后接一 ...

  4. BZOJ 4128 Matrix ——BSGS

    矩阵的BSGS. 只需要哈希一下存起来就可以了. 也并不需要求逆. #include <map> #include <cmath> #include <cstdio> ...

  5. bzoj4128 Matrix 矩阵 BSGS

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4128 题解 想了十分钟没有任何思路. 然后一眼瞥见一句话"数据保证在 \(p\) 内 ...

  6. 【CSS3】 理解CSS3 transform中的Matrix(矩阵)

    理解CSS3 transform中的Matrix(矩阵) by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu ...

  7. 理解CSS3 transform中的Matrix(矩阵)

    一.哥,我被你吓住了 打架的时候会被块头大的吓住,学习的时候会被奇怪名字吓住(如“拉普拉斯不等式”).这与情感化设计本质一致:界面设计好会让人觉得这个软件好用! 所以,当看到上面“Matrix(矩阵) ...

  8. HDU5015 233 Matrix(矩阵高速幂)

    HDU5015 233 Matrix(矩阵高速幂) 题目链接 题目大意: 给出n∗m矩阵,给出第一行a01, a02, a03 ...a0m (各自是233, 2333, 23333...), 再给定 ...

  9. 理解CSS3 transform中的Matrix(矩阵)——张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2427 一.哥,我被你 ...

随机推荐

  1. SQL SERVER 数据有CHAR(10),CHAR(13),CHAR(9)隐藏字符

    原文:SQL SERVER 数据有CHAR(10),CHAR(13),CHAR(9)隐藏字符 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Givi ...

  2. P1417 烹调方案(思维+01背包)

    (点击此处查看原题) 题意 有n种食材,每种食材有三个属性,ai,bi和ci,如果在t时刻完成第i样食材则得到ai-t*bi的美味指数,用第i件食材做饭要花去ci的时间.问在T时间内,什么样的烹调方案 ...

  3. Forest Program(dfs方法---树上的环)

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=6736 沙漠中的每一个连通块都是一棵仙人掌:一个连通块是一棵仙人掌当且仅当连通块中不存在重边和自环,并且每一 ...

  4. Xshell~工作中访问Linux服务器

    1.下载Xshell 下载地址:https://xshell.en.softonic.com/ 2.安装(无特殊修改,自行安装即可) 3.使用 登录(1.新建->输入主机IP,点击确定-> ...

  5. 关于HA(2.102 -2.103 服务器排障)

    关于处理RHCA故障的报告: ,2.102 和 2.103 两台机器在重启之后拉不起来 原因是这两台服务比较怪 先要启动service rpcbind restart 然后再要起service nfs ...

  6. JS基础_if语句

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

  7. O055、Detach Volume 操作

    参考https://www.cnblogs.com/CloudMan6/p/5636510.html     本节我们开始学习 Volume Detach 操作,就是将Volume从Instance上 ...

  8. O040、Migrate Instance 操作详解

    参考https://www.cnblogs.com/CloudMan6/p/5538599.html   Migrate 操作的作用是将instance 从当前的计算节点迁移到其他的计算节点上.   ...

  9. O026、Nova组件详解

    参考https://www.cnblogs.com/CloudMan6/p/5436855.html   本节开始,我们将详细讲解 Nova 的各个子服务.   前面架构概览一节知道 Nova 有若干 ...

  10. javascript实现网页分享至朋友圈功能

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