【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. Day14 Python基础之os/sys/hashlib模块(十二)

    os模块 os.getcwd()  #获取当前工作路径 os.chdir(‘dirname1/dirname2/dirname3’) #改变当前脚本的工作路径,相当于cmd下的cd os.makedi ...

  2. Mysql 索引问题集锦

    一.Mysql 中的索引 索引:顾名思义用来检索.查找数据的key (字段) 几种Mysql 中的常见索引分类:普通索引(联合索引).唯一索引.主键索引.全文索引 优点:使得查询数据变快 缺点:更新数 ...

  3. mysql创建数据库命令

    CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

  4. PAT L2-009 抢红包

    https://pintia.cn/problem-sets/994805046380707840/problems/994805066890854400 没有人没抢过红包吧…… 这里给出N个人之间互 ...

  5. source map

    一.source map 概述 我们在打包中,将开发环境中源代码经过压缩,去空格,babel编译转化,最终可以得到适用于生产环境的项目代码,这样处理后的项目代码和源代码之间差异性很大,会造成无法deb ...

  6. C#实现,C++实现,JS实现 阿拉伯数字金额转换为中文大写金额

    推荐在线编译器  ideone 1. C#实现 :带有负数处理 //把数字金额转换成中文大写数字的函数 //带有负值处理 function changeNumMoneyToChinese(money) ...

  7. java.lang(StringBuffer)

    public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharS ...

  8. day 7-2 multiprocessing开启多进程

    一. multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu\_count\(\)查看),在python中大部分情况需要使用多 ...

  9. js关闭当前页

    /*关闭当前页*/ function closeCurrentPage() { var userAgent = navigator.userAgent; if (userAgent.indexOf(& ...

  10. MVC最全jar包

    <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> & ...