bzoj4128 Matrix
传送门: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的更多相关文章
- BZOJ4128 Matrix 【BSGS】
BZOJ4128 Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...
- BZOJ4128: Matrix(BSGS 矩阵乘法)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 813 Solved: 442[Submit][Status][Discuss] Descriptio ...
- bzoj4128 Matrix 矩阵 BSGS
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4128 题解 想了十分钟没有任何思路. 然后一眼瞥见一句话"数据保证在 \(p\) 内 ...
- (ex)BSGS题表
学了一下BSGS大概知道他是什么了,但是并没有做什么难题,所以也就会个板子.普通的BSGS,我还是比较理解的,然而exBSGS我却只理解个大概,也许还会个板子......(这个东西好像都会有一群恶心的 ...
- 【BZOJ4128】Matrix BSGS+hash
[BZOJ4128]Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...
- 【bzoj4128】Matrix 矩阵乘法+Hash+BSGS
题目描述 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) 输入 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * n的矩阵A.接下来一个n * n的矩阵B 输出 输出 ...
- bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希
题目 给定矩阵A, B和模数p,求最小的正整数x满足 A^x = B(mod p). 分析 与整数的离散对数类似,只不过普通乘法换乘了矩阵乘法. 由于矩阵的求逆麻烦,使用 $A^{km-t} = B( ...
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- 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 ...
随机推荐
- Myeclipse报错-Java compiler level does not match 完美解决方法
从别的地方导入一个项目的时候,经常会遇到eclipse/Myeclipse报Description Resource Path Location Type Java compiler level d ...
- 深入浅出 Webpack
深入浅出 Webpack 评价 Webpack 凭借强大的功能与良好的使用体验,已经成为目前最流行,社区最活跃的打包工具,是现代 Web 开发必须掌握的技能之一.作者结合自身的实战经验,介绍了 Web ...
- jmeter设置全局变量的方法
需求: 同一个线程组内有两个http请求A.B,A请求的后置处理器中存储的有值,B请求中添加用户变量Va先要引用该值,然后B请求的前置处理器再引用用户变量va. 第一种方式: 1.A请求后置处理添加如 ...
- 玩转Node.js(四)-搭建简单的聊天室
玩转Node.js(四)-搭建简单的聊天室 Nodejs好久没有跟进了,最近想用它搞一个聊天室,然后便偶遇了socket.io这个东东,说是可以用它来简单的实现实时双向的基于事件的通讯机制.我便看了一 ...
- nmon Analyser分析仪
nmon Analyser官网: https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Power+System ...
- 第一篇 Postman的初级使用之设置环境快速切换生成环境与测试环境
POSTMAN是有谷歌的开源工具,在开发调试.测试执行过程中使用频率非常广泛,本文将记录一些postman在测试中常见的一些配置和使用方法 一.基本的页面区域 略,很简单,大家都会看,再有,学习下面的 ...
- restAssured + TestNG测试接口,以下是一个get 请求。
package Elaine.Test.G.APITest; import org.testng.Assert;import org.testng.annotations.BeforeTest;imp ...
- 分布式一致性算法之Paxos原理剖析
概述 Zookeeper集群中,只有一个节点是leader节点,其它节点都是follower节点(实际上还有observer节点,不参与选举投票,在这里我们先忽略,下同).所有更新操作,必须经过lea ...
- Word2Vec词向量(一)
一.词向量基础(一)来源背景 word2vec是google在2013年推出的一个NLP工具,它的特点是将所有的词向量化,这样词与词之间就可以定量的去度量他们之间的关系,挖掘词之间的联系.虽然源码是 ...
- LeetCode - 3. Longest Substring Without Repeating Characters(388ms)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...