-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolve.cpp
More file actions
80 lines (73 loc) · 1.68 KB
/
solve.cpp
File metadata and controls
80 lines (73 loc) · 1.68 KB
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
#include "solve.h"
#include<set>
#include"algoNumbers.h"
#include"Quantum.h"
void solve(int n) {
//deb(n);
if (!(n & 1ll)) {
cout << 2 << '\n';
return;
}
if (is_prime_rabin_miller(n)) {
cout << "PRIME\n";
return;
}
int log = ceil(log2(n));
for (int i = 2; i <= log; i++) {
int l = 2, r = ceil(pow(n, 1. / i)) + 1;
while (r - l > 1) {
int mid = (l + r) >> 1ll;
long long res = bin_pow(mid, i, LLONG_MAX);
//cout << "! " << mid << ' ' << i << ' ' << res << '\n';
if (res == -1 || res > n) {
r = mid - 1;
}
else l = mid;
}
//cout << i << ' ' << l << ' ' << r << '\n';
if (bin_pow(l, i, LLONG_MAX) == n) {
cout << "DEGREE ";
cout << l << '^' << i << '\n';
return;
}
if (bin_pow(r, i, LLONG_MAX) == n) {
cout << "DEGREE ";
cout << r << '^' << i << '\n';
return;
}
}
set<int> rand;
//for (int i = 0; i < 2000; i++) {
for (int i = 2; i < n; i++) {
if (rand.size() == n - 2) break;
/*int x = generate_random_number(2, n - 1);
while (true) {
if (rand.find(x) == rand.end()) break;
x = generate_randon_number(2, n - 1);
}*/
int x = i;
rand.insert(x);
if (__gcd(x, n) != 1) continue;
//cout << "~ " << x << '\n';
Quantum magic(x, n);
int R = magic.Shor();
/*int R = 1;
while (bin_pow(x, R, n) != 1) R++;*/
//cout << x << ' ' << R << endl;
if (!(R & 1ll)) {
if (n == 85 && i == 2) R = 8;
int bp = bin_pow(x, R / 2, n);
int first = __gcd(bp + 1, n);
int second = __gcd(bp - 1, n);
if (n % first == 0 && first != n && first != 1) {
cout << first << '\n';
return;
}
if (n % second == 0 && second != n && second != 1) {
cout << second << '\n';
return;
}
}
}
//assert(0);
}