-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBabyStepGiantStep.cpp
84 lines (77 loc) · 1.16 KB
/
BabyStepGiantStep.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define LD long double
#define gc getchar_unlocked
#define pc putchar_unlocked
#define MOD 1000000007
#define MAXN 2000001
#define bitcount _builtin_popcount
#define INF 2000000000
#define EPS 1e-9
template<typename T>T absll(T X)
{
if(X<0)
return -1*X;
else
return X;
}
LL modPow(LL A,LL B,LL M)
{
LL X=1LL;
while(B)
{
if(B&1)
{
X=(X*A)%M;
}
A=(A*A)%M;
B/=2LL;
}
return X%M;
}
//Finds X in A^X=B(mod M)
LL baby_step_giant_step(LL A,LL B,LL M)
{
LL sol=-1;
LL N=(LL)sqrt((double)M+0.0000)+1;
map<LL,LL> val;
for(LL i=N;i>=1;--i)
{
val[modPow(A,i*N,M)]=i;
}
LL maxi=LLONG_MAX;
for(LL i=0;i<=N;i++)
{
LL cur=(modPow(A,i,M)*B)%M;
if(val.count(cur))
{
LL ans=val[cur]*N-i;
maxi=min(maxi,ans);
}
}
//cout<<"maxi="<<maxi<<endl;
if(maxi==LLONG_MAX||modPow(A,maxi,M)!=B)
{
return -1;
}
else
{
return maxi;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL A,B,C;
scanf("%lld %lld %lld",&A,&B,&C);
printf("%lld\n",baby_step_giant_step(A,B,C));
}
return 0;
}