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. Java小技巧输出26个英文字母

    相信有的童鞋写到过与字母有关的小东西,是否有写过全部的字母呢?26个这么多字母,一个个打会疯掉.所有咱们可以用一个小技巧使用for循环帮我们把26个字母自动搞出来,大家来瞅一眼把! 使用Java遍历2 ...

  2. spiral matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  3. MTCNN人脸检测 附完整C++代码

    人脸检测 识别一直是图像算法领域一个主流话题. 前年 SeetaFace 开源了人脸识别引擎,一度成为热门话题. 虽然后来SeetaFace 又放出来 2.0版本,但是,我说但是... 没有训练代码, ...

  4. 移动端调试技巧(禁止webviuew,inspect等)

    如果由于某种原因(天朝FQ),不能支持google 的 inspect  调试 或者再想在某个APP里面调试你的页面,但是没有打开APP的webview ,也不能授权调试 在或者,Fider 可以拦截 ...

  5. 使用 CSS3 实现 3D 图片滑块效果

    Slicebox – A fresh 3D image slider with graceful fallback 英文原文地址:http://tympanus.NET/codrops/2011/09 ...

  6. lvs+keepalive实现主从效果,以及RS健康监测和tcp,udp实现非web的负载均衡

    前面文章讲到了tcp和udp负载均衡,但是没有健康监测,这几天我优化了一下上次的操作.当然,我也是用的跨网段的通讯,因为线上业务主要是海外业务,所以做了iptables流量转发 IP: lvs-mas ...

  7. JDBC、DBUtils

    JDBC(Java Data Base Connectivity) java数据连接 可以为多种数据库,提供统一访问,它由一组用java语言编写的类和接口组成,也是java访问数据库的规范.   my ...

  8. Python_添加行号

    filename='demo.py' with open(filename,'r')as fp: lines=fp.readlines() #读取所有行 maxLength=max(map(len,l ...

  9. Python_字符串格式化

    #冒泡排序 array = [1,2,3,6,5,4] for i in range(len(array)): for j in range(i): if array[j] > array[j ...

  10. hexo+github创建属于自己的博客

    配置环境 安装Node(必须) 作用:用来生成静态页面的 到Node.js官网下载相应平台的最新版本,一路安装即可. 安装Git(必须) 作用:把本地的hexo内容提交到github上去. 安装Xco ...