传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128

【题解】

矩阵版本的BSGS。

至于如何不需要求逆,详见:http://www.cnblogs.com/galaxies/p/bzoj2480.html

# include <map>
# include <math.h>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + ; # define RG register
# define ST static int n, mod; struct matrix {
int n, a[][];
inline void init(int _n) {
n = _n;
memset(a, , sizeof a);
}
inline void set(int _n) {
n = _n;
for (int i=; i<=n; ++i)
for (int j=; j<=n; ++j)
scanf("%d", &a[i][j]);
}
friend matrix operator * (matrix a, matrix b) {
matrix c; c.init(a.n);
for (int i=; i<=a.n; ++i)
for (int j=; j<=a.n; ++j)
for (int k=; k<=a.n; ++k) {
c.a[i][j] += 1ll * a.a[i][k] * b.a[k][j] % mod;
if(c.a[i][j] >= mod) c.a[i][j] -= mod;
}
return c;
}
friend matrix operator ^ (matrix a, int b) {
matrix c; c.init(a.n);
for (int i=; i<=a.n; ++i) c.a[i][i] = ;
while(b) {
if(b&) c = c * a;
a = a * a;
b >>= ;
}
return c;
}
friend bool operator == (matrix a, matrix b) {
for (int i=; i<=a.n; ++i)
for (int j=; j<=a.n; ++j)
if(a.a[i][j] != b.a[i][j]) return ;
return ;
}
inline ull ghash() {
ull ret = ;
for (int i=; i<=n; ++i)
for (int j=; j<=n; ++j)
ret = ret * 20001130ull + a[i][j];
return ret;
}
}A, B; map<ull, int> mp; inline int BSGS(int P) {
mp.clear();
int m = ceil(sqrt(1.0 * P));
matrix t = B; ull tem;
for (int i=; i<m; ++i) {
mp[t.ghash()] = i;
t = t * A;
}
matrix g = A^m; t = g;
for (int i=; i<=m+; ++i) {
tem = t.ghash();
if(mp.count(tem)) return i * m - mp[tem];
t = t * g;
}
return -;
} int main() {
cin >> n >> mod;
A.set(n);
B.set(n);
cout << BSGS(mod) << endl;
return ;
}

bzoj4128 Matrix的更多相关文章

  1. BZOJ4128 Matrix 【BSGS】

    BZOJ4128 Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...

  2. BZOJ4128: Matrix(BSGS 矩阵乘法)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 813  Solved: 442[Submit][Status][Discuss] Descriptio ...

  3. bzoj4128 Matrix 矩阵 BSGS

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

  4. (ex)BSGS题表

    学了一下BSGS大概知道他是什么了,但是并没有做什么难题,所以也就会个板子.普通的BSGS,我还是比较理解的,然而exBSGS我却只理解个大概,也许还会个板子......(这个东西好像都会有一群恶心的 ...

  5. 【BZOJ4128】Matrix BSGS+hash

    [BZOJ4128]Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...

  6. 【bzoj4128】Matrix 矩阵乘法+Hash+BSGS

    题目描述 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) 输入 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * n的矩阵A.接下来一个n * n的矩阵B 输出 输出 ...

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

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

  8. angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation

    今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:

  9. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

随机推荐

  1. stm32--FatFs调试过程(SPIFlash)

    移植方法参见我的另一篇博客:<stm32--FatFs移植(SPIFlash)>. 本文仅记录在初次移植完成后,遇到的问题,和解决的过程. 调试记录: 问题1:f_open返回3,即磁盘没 ...

  2. cordova 框架下开发app推送

    cordova提供官方的push pluging,使用的是Google的GCM消息推送服务,一些网络原因,国内GCM可能不怎么好用.所以选择国内的第三方插件. 可供选择的有百度云推送,腾讯云信鸽,极光 ...

  3. jmeter3.2版本如何进行webservice接口功能测试

    jmeter3.2版本之后就没有SOAP/XML-RPC Request插件了,所以没办法直接进行webservice接口的测试. 原理上: Web service一般就是用SOAP协议通过HTTP来 ...

  4. jsp 中获取自定义变量

    首先确定El表达式的查找范围: 依次从Page.Request.Session.Application 中的 attribute属性中查找. <% String basePath = reque ...

  5. Linux常用命令及搭建测试环境

    题外话:三大操作系统------Linux.Unix.Windows,Unix系统如常见的Mac OS,Linux的很多命令跟Unix是通用的,所以就有一些开发人猿喜欢用苹果的原因.Linux发行版特 ...

  6. AGV系统上位机--工程案例【1】

    1.满足80%系统需求,根据需求生成任务表单 2.指定小车下方任务 3.项目实战应用 4.后台开发,对接其他平台,简易实现自动生成任务列表,自动排单 5.AGV系统上位机初学者很容易理解上手 6.欢迎 ...

  7. Ubuntu 常见错误及解决方法——长期不定时更新

    1. 修复 /etc/sudoers 文件损坏导致不能使用 sudo 命令 这是之前错误地编辑了 /etc/sudoers 这个文件导致的,因此撤销编辑即可,但由于已经不能使用 sudo 命令,因此不 ...

  8. ORM选型对比

    ORM框架选型 ORM框架选型 jian A YEAR AGO (2017-04-10) orm, database 选型标准:实现O/R mapping,基于promise,支持原生SQL语句,支持 ...

  9. MongoDB的复制一:复制的原理

    1.复制的角色 复制有三种角色: primay:主库,执行所有的写操作,并把日志写入oplog里. secondary:复制主库的所有操作.读取主库的oplog,并执行日志里的内容.默认情况下,客户端 ...

  10. 浅谈C#中show和showDialog的区别

    [转载] A.WinForm中窗体显示  显示窗体可以有以下2种方法:  Form.ShowDialog方法 (窗体显示为模式窗体)  Form.Show方法 (窗体显示为无模式窗体) 2者具体区别如 ...