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 一.哥,我被你 ...
随机推荐
- 2019-07-30 C#基础知识学习
继承和多态 接口与抽象类的区别:1.在接口中仅能定义成员,但是不能有具体的实现:抽象类除了抽象成员以外,其他成员有具体的实现.2.在接口中不能声明字段,并且不能声明任何私有成员,成员不能包含任何修饰符 ...
- ubuntu 设置静态ip,但显示scope global secondary ens33
设置静态ip 修改 /etc/network/interfaces 文件 # The loopback network interface auto lo iface lo inet loopback ...
- 使用foreach的禁忌
List<String> list = new ArrayList<>(); Iterator<String> iterator = list.iterator() ...
- 基于 CentOS 7 搭建 SVN
⒈安装 SVN 服务端 1.安装 Subversion Subversion 是一个版本控制系统,相对于的 RCS . CVS ,采用了分支管理系统,它的设计目标就是取代 CVS . yum inst ...
- 安装 pybloomfilter
1.在windows的cmd下,使用 pip install pybloomfiltermmap 命令安装,pybloomfiltermmap 时报错 ,错误信息如下 根据错误信息分析,报错原因是需要 ...
- Docker-PS命令解析
查看 docker 容器,必然要用到 docker ps 命令.其基本格式为: docker ps [OPTIONS] 关键在于 OPTIONS(选项): 1 常见用法 1. 最常见的用法 $ doc ...
- mysql+canal+kafka+elasticsearch构建数据查询平台
1. 实验环境 CPU:4 内存:8G ip:192.168.0.187 开启iptables防火墙 关闭selinux java >=1.5 使用yum方式安装的java,提前配置好JAVA_ ...
- 在Global.asax中 注册Application_Error事件 捕获全局异常
参考于:https://shiyousan.com/post/635813858052755170 在ASP.NET MVC中,通过应用程序生命周期中的Application_Error事件可以捕获到 ...
- Oracle数据库中的变量
Oracle数据库中的变量 来源:https://blog.csdn.net/wahaa591/article/details/46772769 1.define(即host变量) define va ...
- 如何为 esp32 编译和配置及烧写 MicroPython 固件。
MicroPython 在 esp-idf (esp32) 上编译固件 esp32 编译 micropython 的固件相关的资料应该很多吧,我也会出一篇,但会额外讲一些 linux 的东西的. 资料 ...