-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpollard_rho.cpp
More file actions
189 lines (186 loc) · 3.61 KB
/
pollard_rho.cpp
File metadata and controls
189 lines (186 loc) · 3.61 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int __int128
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 1000001
#define mod 998244353
int read() // __int128 functions
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
void print(__int128 x) // __int128 functions
{
if (x < 0)
{
cout << "-";
x = -x;
}
stack<char> s;
while (x)
{
s.push((x % 10) + '0');
x = x / 10;
}
while (!s.empty())
{
cout << s.top();
s.pop();
}
}
namespace pollard_rho
{
int multiplicate(int x, int y, int m)
{
return (x * y) % m;
}
int modpow(int x, int y, int m)
{
int z = 1;
while (y)
{
if (y & 1)
z = (z * x) % m;
x = (x * x) % m;
y >>= 1;
}
return z;
}
bool is_composite(int n, int a, int d, int s)
{
int x = modpow(a, d, n);
if (x == 1 or x == n - 1)
return false;
for (int r = 1; r < s; r++)
{
x = multiplicate(x, x, n);
if (x == n - 1LL)
return false;
}
return true;
};
int miller_rabin(int n)
{
if (n < 2)
return false;
int r = 0, d = n - 1LL;
while ((d & 1LL) == 0)
{
d >>= 1;
r++;
}
for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37})
{
if (n == a)
return true;
if (is_composite(n, a, d, r))
return false;
}
return true;
}
int f(int x, int m)
{
return multiplicate(x, x, m) + 1;
}
int rho(int n)
{
int x0 = 1, t = 0, prd = 2;
int x = 0, y = 0, q;
while (t % 40 || __gcd(prd, n) == 1)
{
if (x == y)
{
x0++;
x = x0;
y = f(x, n);
}
q = multiplicate(prd, max(x, y) - min(x, y), n);
if (q != 0)
prd = q;
x = f(x, n);
y = f(y, n);
y = f(y, n);
t++;
}
return __gcd(prd, n);
}
vector<int> fact(int n)
{
if (n == 1)
return {};
if (miller_rabin(n))
return {n};
int x = rho(n);
auto l = fact(x), r = fact(n / x);
l.insert(l.end(), r.begin(), r.end());
return l;
}
}
signed main()
{
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
while (1)
{
int n = read();
if (n == 0)
break;
vector<int> factors = pollard_rho::fact(n);
sort(factors.begin(), factors.end());
int prev = -1, cnt = 0;
for (auto const &i : factors)
{
if (prev != i)
{
if (prev != -1)
{
print(prev);
printf("^");
print(cnt);
printf(" ");
}
prev = i;
cnt = 0;
}
cnt++;
}
if (prev != -1)
{
print(prev);
printf("^");
print(cnt);
printf(" ");
}
printf("\n");
}
return 0;
}
// sources:
// https://github.com/PauloMiranda98/Competitive-Programming-Notebook/blob/master/code/math/prime.h
// https://github.com/brunomaletta/Biblioteca/blob/master/Codigo/Matematica/pollardrho.cpp
// fast integer factorization with pollard-rho
// https://www.spoj.com/problems/FACT0/ - ok
// https://www.spoj.com/problems/FACT1/ - ok
// https://www.spoj.com/problems/FACT2/ - sigkill
// since the limit is at most 29 digits(in FACT2), we need to use __int128