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