传送门: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. leetcode笔记--6 Add Digits

    question: Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  2. LeetCode高频题目(100)汇总-Java实现

    LeetCode高频题目(100)汇总-Java实现       LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...

  3. 揭秘css

    这是我看到非常好的一本电子教程,可以当参考手册使用,链接

  4. 在PXC中重新添加掉线节点

      Preface       When we add a new node into PXC structure,it will estimate the mothed(IST/SST) to tr ...

  5. 【个人笔记】关于C++小数的处理

    无论是C-Style还是C++-Style的输出,小数都会四舍五入.如果想要截断两种比较好的方法.第一种:利用sscanf输出成字符串,再人为地putchar().第二种:已知钦定保留6位小数,那么可 ...

  6. Ubuntu 安装Google浏览器

    Ubuntu自带的浏览器是火狐浏览器,使用的时候多多少少有些不方便,这里安装Googel浏览器. 下载 可以到 Ubuntu chrome去下载安装包. 安装 首先到下载的根目录 cd ~/Downl ...

  7. Appium如何获取appPackage和appActivity

    基本概念: appPackage:简单来说是App开发者提供的名称. appActivity:简单来说是App提供的各种不同的功能.每个程序都有个MainActivity,就是打开程序时显示在屏幕的活 ...

  8. 重写selenium 的 click()操作,使其变成隐式等待

    selenium 页面常会因为页面加载慢而出现element 不能被点击到的情况,比如加载过程中出现遮罩,导致element 可见不可点.以下方法重写click(),用隐式等待解决这个问题. 基本思路 ...

  9. (原创)像极了爱情的详解排序二叉树,一秒get

    排序二叉树(建立.查找.删除) 二叉树我们已经非常熟悉了,但是除了寻常的储存数据.遍历结构,我们还能用二叉树做什么呢? 我们都知道不同的遍历方式会对相同的树中产生不同的序列结果,排序二叉树就是利用二叉 ...

  10. Toward Convolutional Blind Denoising of Real Photographs

    本文提出了一个针对真实图像的盲卷积去噪网络,增强了深度去噪模型的鲁棒性和实用性. 摘要 作者提出了一个 CBD-Net,由噪声估计子网络和去噪子网络两部分组成. 作者设计了一个更加真实的噪声模型,同时 ...