forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3243 - Clever Y.cpp
90 lines (67 loc) · 2.22 KB
/
3243 - Clever Y.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
84
85
86
87
88
89
90
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
int pow(long long a, int b, int c){
long long ret = 1;
while(b){
if(b & 1) ret = ret * a % c;
a = a * a % c;
b >>= 1;
}
return ret;
}
int r;
pair<int, int> baby[31622],giant[31622];
bool cmp(pair<int, int> a, pair<int, int> b){
if(a.first != b.first) return a.first < b.first;
return a.second > b.second;
}
int main(){
int x,z,m;
while(true){
scanf("%d %d %d",&x,&m,&z);
if(x == 0) break;
z %= m;
if(m == 1 || z == 1) puts("0");
else{
int ans = -1;
if(pow(x,31,m) == 0){
long long aux = 1;
for(int i = 0;i < 32;++i){
if(aux == z){
ans = i;
break;
}
aux = x * aux % m;
}
}else{
r = (int)floor(sqrt(m));
int max_val = (m + r - 1) / r;
long long p = z;
for(int i = 0;i < r;++i){
baby[i] = make_pair(p % m,i);
p = p * x % m;
}
sort(baby,baby + r,cmp);
int aux = pow(x,r,m);
p = aux;
for(int i = 0,e = r;i < max_val;++i, e += r){
giant[i] = make_pair(p,e);
p = p * aux % m;
}
sort(giant,giant + max_val);
for(int i = 0,j = 0;i < max_val && j < r;++i){
while(j < r && baby[j].first < giant[i].first) ++j;
if(j < r && baby[j].first == giant[i].first)
if(ans == -1 || giant[i].second - baby[j].second < ans)
ans = giant[i].second - baby[j].second;
}
}
if(ans == -1) puts("No Solution");
else printf("%d\n",ans);
}
}
return 0;
}