BSGS算法,预处理出ϕ(c)−−−−√内的a的幂,每次再一块一块的往上找,转移时将b乘上逆元,哈希表里O(1)查询即可

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#define LL long long
long long a,b,c,m;
bool bo=0;
std::map<long long,int> pp;
std::map<long long,bool> vis;
LL exgcd(LL o,LL p,LL &x,LL &y){
if(p==0){
x=1;y=0;
return o;
}
LL gcd=exgcd(p,o%p,x,y);
LL t=x;
x=y;
y=t-(o/p)*x;
return gcd;
}
int main(){
while(scanf("%lld%lld%lld",&c,&a,&b)==3){
pp.clear(); vis.clear(); bo=0;
if(a%c==0){printf("no solution\n");continue;}
m=(LL)ceil(sqrt((double)c));
LL now=1;
pp[now]=0; vis[now]=1;
for(int i=1;i<m;i++){
now=(now*a)%c;
if(!vis[now]){vis[now]=1;pp[now]=i;}
}
now=(now*a)%c;
LL x,y;
LL d=exgcd(now,c,x,y);
x=(x%c+c)%c;
for(int i=0;i<=m;i++){
if(vis[b]){
printf("%lld\n",i*m+pp[b]);
bo=1; break;
}
b=(b*x)%c;
}
if(bo==1)continue;
printf("no solution\n");
}
return 0;
}

bzoj 3239 poj 2417 BSGS的更多相关文章

  1. POJ 2417 Discrete Logging BSGS

    http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...

  2. BSGS算法+逆元 POJ 2417 Discrete Logging

    POJ 2417 Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4860   Accept ...

  3. BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)

    我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...

  4. POJ - 2417 Discrete Logging(Baby-Step Giant-Step)

    d. 式子B^L=N(mod P),给出B.N.P,求最小的L. s.下面解法是设的im-j,而不是im+j. 设im+j的话,貌似要求逆元什么鬼 c. /* POJ 2417,3243 baby s ...

  5. BZOJ 3239 Discrete Logging(BSGS)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...

  6. bzoj 3239: Discrete Logging && 2480: Spoj3105 Mod【BSGS】

    都是BSGS的板子题 此时 \( 0 \leq x \leq p-1 \) 设 \( m=\left \lceil \sqrt{p} \right \rceil ,x=i*m-j \)这里-的作用是避 ...

  7. POJ 2417 Discrete Logging 离散对数

    链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...

  8. poj 2417 Discrete Logging(A^x=B(mod c),普通baby_step)

    http://poj.org/problem?id=2417 A^x = B(mod C),已知A,B.C.求x. 这里C是素数,能够用普通的baby_step. 在寻找最小的x的过程中,将x设为i* ...

  9. bzoj 3122 随机数生成器 - BSGS

    Description Input 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数.   接下来T行,每行有五个整数p,a,b,X1,t,表示一组数据.保证X1和t都是合法的页码. ...

随机推荐

  1. Python__flask初识

    1.  debug:在app.run()里面加上app.run(debug=True), 在浏览器中调试的时候可以直接显示出错误. 2.  在url中传递参数,可以这样 @app.route('/ch ...

  2. 排序算法入门之归并排序(java实现)

    归并排序是采用分治法的典型应用. 参考<数据结构与算法分析-Java语言描述> 归并排序其实要做两件事: (1)"分解"--将序列每次折半划分. (2)"合并 ...

  3. DB2常见问题

    15.1实例常见问题和诊断案例 1.实例无法启动问题 db2nodes.cfg文件,主要是为了数据库分区设计的.如果实例无法启动,要检查db2nodes.cfg,看配置是否正常.db2systm实例配 ...

  4. Windows10上搭建Kinect 2 开发环境

    因为Visual Studio 2017的应用最低只能面向windows10,而Kinect SDK 2.0的系统版本要求是windows 8,所以不得不下载Visual Studio 2013 co ...

  5. jtds驱动更新对一个老问题的解决

    07年年末的一篇blog: 以前网站做初期开发时,有一个问题:hibernate下text大字符串读取时出这个异常:JDBCExceptionReporter - The amount of data ...

  6. Nginx日志配置及配置调试

    防火墙内的内网服务器,因为网关传过来的remot_addr都一样,不得不对Nginx的日志格式做了配置 配置语法如下: log_format  myformat  '$http_x_forwarded ...

  7. 学习spring中遇见的问题

    报错: Unexpected exception parsing XML document from class path resource [applicationContext.xml]; nes ...

  8. 爬虫之urllib包

    urllib简介 简介 Python3中将python2.7的urllib和urllib2两个包合并成了一个urllib库 Python3中,urllib库包含有四个模块: urllib.reques ...

  9. Java 线程同步组件 CountDownLatch 与 CyclicBarrier 原理分析

    1.简介 在分析完AbstractQueuedSynchronizer(以下简称 AQS)和ReentrantLock的原理后,本文将分析 java.util.concurrent 包下的两个线程同步 ...

  10. Leetcode_删除排序数组中的重复项

    Leetcode  删除排序数组中的重复项 题目: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用 额外的数组空间,你必须在原地修改输入数 ...