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. jmeter-提取器之JSON Path PostProcessor

    后置处理器添加 json path postprocessor. 用处: 当前接口响应返回的json中提取内容,作为变量可以在不同的请求中传递. 1. json path postprocessor ...

  2. postgres_fdw

    create extension postgres_fdw; --创建扩展 create server db0 foreign data wrapper postgres_fdw OPTIONS (h ...

  3. linker 错误解决办法 地图

  4. 获取.net应用的版本及依赖信息

    在制作打包安装器时,通常要获取要安装的程序的名称.版本.说明,以及依赖的版本信息,经过翻阅MSDN,stackoverflow,终于搞定了. 1. 获取应用的依赖信息 var ans = System ...

  5. web综合案例01

    web综合案例01 ... .... 内容待添加

  6. easyui-dialog对话框练习

    <div id="dl1" class="easyui-dialog" title="窗口" style="width:40 ...

  7. EIGRP-3-EIGRP的多参数度量

    带宽度量参数本身无法区分10Gbit/s及更高速率的接口.对1Gbit/s接口,默认延迟度量参数已设置为最低值1(10微妙).而且EIGRP承载的是经过换算的参数,每台路由器需要将其换算回再计算新开销 ...

  8. Unraveling the JPEG file

    (文章还剩实践部分没写,答辩过后补上...) JPEG文件在当下数字化生活中是无处不在的,但是在熟悉的JPEG面纱背后,隐藏着一些算法,它们去除了人类眼中无法察觉到的细节.这产生了最高的视觉质量与最小 ...

  9. Flask虚拟环境连接mysql出现1366的解决方案

    报错信息 Warning: (1366, "Incorrect string value: '\xD6\xD0\xB9\xFA\xB1\xEA...' for column 'VARIABL ...

  10. Angular2.0的学习(三)

    第三节课:依赖注入 1.什么是依赖注入模式及使用依赖注入的好处 2.介绍Angular的依赖注入实现:注入器和提供器 3.注入器的层级结构