BZOJ 4128: Matrix

标签(空格分隔): OI BZOJ 大步小步 矩阵 费马小定理


Time Limit: 10 Sec

Memory Limit: 128 MB


Description

给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p)

Input

第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * n的矩阵A.接下来一个n * n的矩阵B

Output

输出一个正整数,表示最小的可能的x,数据保证在p内有解

Sample Input

2 7

1 1

1 0

5 3

3 2

Sample Output

4

HINT

对于100%的数据,n <= 70,p <=19997,p为质数,0<= A_{ij},B_{ij}< p

保证A有逆


Solution####

大步小步算法\({A^{x}\equiv B\pmod p}\)

设\({A^{a\sqrt{p}-b}\equiv B\pmod p}\)

变换可得\({A^{a\sqrt{p}}\equiv B*A^{b}\pmod p}\)

对\({A^{a\sqrt{p}}\pmod{p}}\)预处理

在hash表中查找和\({B*A^{b}\pmod{p}}\)相同的

时间复杂度\({n^{3}*\sqrt{p}}\)


Code####

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<vector>
using namespace std;
const int N=0,M=0;
int read()
{int s=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0';ch=getchar();}
return s*f;
}
//smile please
int n,P,L;
int cf[70];
struct matrix
{
long long s[70][70];
void one()
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
s[i][j]=(i==j);
}
void readin()
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
s[i][j]=read();
}
int hash()
{
int ss=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
ss=ss*189211+s[i][j];
return ss;
}
void operator*=(matrix b)
{matrix a;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a.s[i][j]=s[i][j],
s[i][j]=0;
for(int j=0;j<n;j++)
{
for(int i=0;i<n;i++)cf[i]=b.s[i][j];
for(int i=0;i<n;i++)
{
for(int k=0;k<n;k++)
s[i][j]+=a.s[i][k]*cf[k];
s[i][j]%=P;
}
}
}
}A,B;
int he[1<<16],hn[1<<16],hv[1<<16],hl[1<<16],hw=1;
void put(int u,int v,int l)
{hw++;hn[hw]=he[u];he[u]=hw;hv[hw]=v;hl[hw]=l;}
int ans=0;
int main()
{
n=read(),P=read();
A.readin();B.readin();
L=sqrt(P);ans=-1;
matrix S=A,k=B;
for(int i=1;i<=L;i++)
{k*=A;if(i!=1)S*=A;
int ha=k.hash();
put(ha&((1<<16)-1),ha,i);
}
matrix SS=S;
for(int i=L;i<=P+L&&ans==-1;i+=L,SS*=S)
{
int ha=SS.hash();
for(int j=he[ha&((1<<16)-1)];j&&ans==-1;j=hn[j])
if(hv[j]==ha)
ans=i-hl[j];
}
if(ans==-1)
printf("no solution\n");
else
printf("%d\n",ans);
return 0;
}

BZOJ 4128: Matrix的更多相关文章

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

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

  2. BZOJ 4128 Matrix BSGS+矩阵求逆

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

  3. BZOJ 4128 Matrix ——BSGS

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

  4. BZOJ 4128: Matrix (矩阵BSGS)

    类比整数的做法就行了 1A爽哉 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int M ...

  5. 【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128 大水题一道 使用大步小步算法,把数字的运算换成矩阵的运算就好了 矩阵求逆?这么基础的线 ...

  6. 【BZOJ】4128: Matrix

    题解 学习一下矩阵求逆 就是我们考虑这个矩阵 \(AA^{-1} = I\) 我们相当于让\(A\)乘上一个矩阵,变成\(I\) 我们可以利用初等行变换(只能应用初等行变换,或只应用初等列变换) 分三 ...

  7. bzoj 4128 矩阵求逆

    /************************************************************** Problem: 4128 User: idy002 Language: ...

  8. BZOJ 2351 Matrix(哈希)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2351 题意:给出一个n*m的01矩阵.再给出10个A*B的小01矩阵.判断这些小的矩阵是 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. Ocelot(三)- 服务发现

    Ocelot(三)- 服务发现 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/10907856.html 源码地址:https ...

  2. 使用imp命令和exp命令对oracle数据库进行导入导出操作

    命令说明 imp命令和exp命令需要在cmd命令窗口下执行,并且需要在系统环境变量中配置imp,exp命令所在目录 导出命令 --全库导出 exp system/systempassword@orcl ...

  3. 在Linux系统下远程连接oracle的防火墙设置

    在Linux系统要远程连接Oracle的防火墙设置方法: 打开5801至5810 端口用于vnc iptables -I INPUT -p tcp --dport 5801:5810 -j ACCEP ...

  4. Window安装配置Redis

    一.下载windows版本的Redis github下载地址:https://github.com/MSOpenTech/redis/tags 二.安装启动Redis Redis 支持 32 位和 6 ...

  5. 继承、super、this、抽象类

    继承.super.this.抽象类 继承.super.this.抽象类 继承.super.this.抽象类 继承.super.this.抽象类 继承.super.this.抽象类

  6. Error: Attribute application@allowBackup value=(false) from AndroidManifest.xml:14:7-34 is also present at [:react-native-qq] AndroidManifest.xml:14:18-44 value=(true).

    解决方法: 修改文件: 在manifest标签中添加 xmlns:tools="http://schemas.android.com/tools" 在application标签中添 ...

  7. iptables端口转发规则(内网端口转外网端口)

    需求:外网124.202.173.118需要访问 10.45.225.70的内网54032端口,10.45.225.70服务器有公网地址139.129.109.81将内网地址端口转发到外网地址端口,并 ...

  8. lintcode-dfs实现二叉树的层序遍历

    class Solution { /** * @param root: The root of binary tree. * @return: Level order a list of lists ...

  9. scala数据类型

    # Scala数据类型 ## 1.数值类型 ### 1.1 与Java一样Scala也有8种数值类型 * Byte * Char * Short * Int * Long * Float * Doub ...

  10. aspnetcore配置log4net并添加全局异常处理

    第一步:在NuGet中引用log4net 第二步:创建log4net.config <?xml version="1.0" encoding="utf-8" ...