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;
}

最新文章

  1. [leetcode 35] Search Insert Position
  2. js函数实现转换css中常用的颜色编码
  3. Divide and Conquer:Monthly Expense(POJ 3273)
  4. [CareerCup] 17.10 Encode XML 编码XML
  5. ADF_ManagedBean的概念和管理(概念)
  6. 关于C++ const 的全面总结
  7. [CF738B]Spotlights(前缀和,模拟)
  8. WCF学习心得------(七)消息协定
  9. (转)《深入理解java虚拟机》学习笔记6——类加载机制
  10. A Simple problem
  11. Mysql bigint 类型转为datetime
  12. 解决:Could not find debuginfo pkg for dependency package glibc-2.12-1.132.el6_5.3.i686
  13. SpringMVC轻松学习-其他常用(四)
  14. C#中的DateTime是值类型还是引用类型
  15. CSS3的[att$=val]选择器
  16. python3-基础7
  17. request和reponse
  18. Php7有哪些新特性:
  19. awk的些许小技巧
  20. 1.关于Swift

热门文章

  1. nifi1.6.0汉化
  2. 详谈linux中压缩
  3. 论MVC中的传值
  4. SpringBoot配置拦截器
  5. Ocelot中文文档-请求聚合
  6. C#本质论笔记
  7. c# 事件和EventManager
  8. Effective C++ 读书笔记(39-45)
  9. android sqlite no such table
  10. 【问题】sql数据库报无效的数据证书,需重新安装