【SPOJ】Power Modulo Inverted(拓展BSGS)

题面

洛谷

求最小的\(y\)

满足

\[k\equiv x^y(mod\ z)
\]

题解

拓展\(BSGS\)模板题

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
const int HashMod=123456;
struct HashTable
{
struct Line{int u,v,next;}e[100000];
int h[HashMod],cnt;
void Add(int u,int v,int w){e[++cnt]=(Line){w,v,h[u]};h[u]=cnt;}
void clear(){memset(h,0,sizeof(h));cnt=0;}
void Insert(int x,int i)
{
int k=x%HashMod;
Add(k,i,x);
}
int Query(int x)
{
for(int i=h[x%HashMod];i;i=e[i].next)
if(e[i].u==x)return e[i].v;
return -1;
}
}Hash;
int fpow(int a,int b,int MOD)
{
int s=1;
while(b){if(b&1)s=1ll*s*a%MOD;a=1ll*a*a%MOD;b>>=1;}
return s;
}
void NoAnswer(){puts("No Solution");}
void ex_BSGS(int y,int z,int p)
{
if(z==1){puts("0");return;}
int k=0,a=1;
while(233)
{
int d=__gcd(y,p);if(d==1)break;
if(z%d){NoAnswer();return;}
z/=d;p/=d;++k;a=1ll*a*y/d%p;
if(z==a){printf("%d\n",k);return;}
}
Hash.clear();
int m=sqrt(p)+1;
for(int i=0,t=z;i<m;++i,t=1ll*t*y%p)Hash.Insert(t,i);
for(int i=1,tt=fpow(y,m,p),t=1ll*a*tt%p;i<=m;++i,t=1ll*t*tt%p)
{
int B=Hash.Query(t);if(B==-1)continue;
printf("%d\n",i*m-B+k);return;
}
NoAnswer();
}
int main()
{
int x,z,k;
while(233)
{
x=read();z=read();k=read();
if(x==0&&z==0&&k==0)break;
ex_BSGS(x,k,z);
}
return 0;
}

【SPOJ】Power Modulo Inverted(拓展BSGS)的更多相关文章

  1. 「SPOJ 3105」Power Modulo Inverted

    「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...

  2. MOD - Power Modulo Inverted(SPOJ3105) + Clever Y(POJ3243) + Hard Equation (Gym 101853G ) + EXBSGS

    思路: 前两题题面相同,代码也相同,就只贴一题的题面了.这三题的意思都是求A^X==B(mod P),P可以不是素数,EXBSGS板子题. SPOJ3105题目链接:https://www.spoj. ...

  3. spoj3105 MOD - Power Modulo Inverted(exbsgs)

    传送门 关于exbsgs是个什么东东可以去看看yyb大佬的博客->这里 //minamoto #include<iostream> #include<cstdio> #i ...

  4. 【POJ 3243】Clever Y 拓展BSGS

    调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那 ...

  5. 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)

    什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...

  6. [拓展Bsgs] Clever - Y

    题目链接 Clever - Y 题意 有同余方程 \(X^Y \equiv K\ (mod\ Z)\),给定\(X\),\(Z\),\(K\),求\(Y\). 解法 如题,是拓展 \(Bsgs\) 板 ...

  7. 数学:拓展BSGS

    当C不是素数的时候,之前介绍的BSGS就行不通了,需要用到拓展BSGS算法 方法转自https://blog.csdn.net/zzkksunboy/article/details/73162229 ...

  8. 【POJ3243】拓展BSGS(附hash版)

    上一篇博文中说道了baby step giant step的方法(简称BSGS),不过对于XY mod Z = K ,若x和z并不互质,则不能直接套用BSGS的方法了. 为什么?因为这时候不存在逆元了 ...

  9. 【POJ3243】【拓展BSGS】Clever Y

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

随机推荐

  1. Dapper.NET

    关于Dapper.NET的相关论述   年少时,为何不为自己的梦想去拼搏一次呢?纵使头破血流,也不悔有那年少轻狂.感慨很多,最近事情也很多,博客也很少更新了,毕竟每个人都需要为自己的生活去努力. 最近 ...

  2. 学习memcache

    本文参考了菜鸟教程中的内容. 安装 安装memcache的时候,请切换为root用户 root@centos # wget http://www.memcached.org/files/memcach ...

  3. php使用gd库输出中文内容的图片

    正如标题所说那样,本文只讨论输出内容全部为中文或者包含中文的情况.如果内容全是字母或者其他字符的话,可以参考这篇博客:生成验证码 问题 此处要注意,标题中为什么要区别windows和linux分别实现 ...

  4. centos7 network eno16777736

    Network service not running - eno16777736 not activated - CentOShttps://www.centos.org/forums/viewto ...

  5. Python解释器有哪些?Python解释器种类

    Python是一门解释器语言,代码想运行,必须通过解释器执行,Python存在多种解释器,分别基于不同语言开发,每个解释器有不同的特点,但都能正常运行Python代码,以下是常用的五种Python解释 ...

  6. react插件包

    react-scoped-style support ie8,ie8+,chrome,firefox,safari does not support css priority (just apply ...

  7. IdentityServer4【Introduction】之概括

    The Big Picture 大多数现代应用看起来都像下面的样子: 大多数的交互是下面这样: 浏览器与web应用之间的通信 web应用和web APIs之间的通信(这两者有时是独立的,有时是有用户参 ...

  8. 《Effective C++》设计与声明:条款18-条款25

    条款18:让接口容易被正确使用,不容易被误用 注意使用const,explicit,shared_ptr等来限制接口. 必要时可以创建一些新的类型,限制类型操作,束缚对象等. 注意保持接口的一致性,且 ...

  9. springmvc配置文件的主要内容

    springmvc配置文件的主要内容:

  10. 运行Spark-shell,解决Unable to load native-hadoop library for your platform

    启动spark后,运行bin/spark-shell会出现一个警告 提君博客原创 WARN util.NativeCodeLoader: Unable to load native-hadoop li ...