Skip to content

Commit 8efa0ac

Browse files
authored
Fixed implementation errors
1 parent 12d93d6 commit 8efa0ac

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

math/fast_pollard_rho.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ inline u128 mul(u128 x, u128 y, u128 c)
1818
return ans % c;
1919
}
2020

21+
u128 HI(u128 x) { return x>>64; };
22+
u128 LO(u128 x) { return u64(x); };
23+
2124
namespace Prime {
2225

2326
int const num_tries = 20;
@@ -55,7 +58,7 @@ namespace Prime {
5558
u128 s = 1, d = x>>1;
5659
while (!(d&1)) s++, d>>= 1;
5760

58-
uniform_int_distribution<u64> rng(2, u64(x)-1);
61+
uniform_int_distribution<u64> rng(2, HI(x)? u64(-1ull): (u64(x)-1));
5962

6063
for (int i = 0; i < num_tries; i++) {
6164
u64 p = rng(rd);
@@ -82,10 +85,7 @@ namespace Factor {
8285
x*=x;
8386
}
8487
}
85-
86-
u128 HI(u128 x) { return x>>64; };
87-
u128 LO(u128 x) { return u64(x); };
88-
88+
8989
struct u256 {
9090
u128 hi, lo;
9191

@@ -110,7 +110,7 @@ namespace Factor {
110110

111111
inline u128 gcd(u128 a, u128 b) {
112112
auto ctz = [] (u128 x) {
113-
if (u64(x)) return __builtin_ctzll(x);
113+
if (!HI(x)) return __builtin_ctzll(x);
114114
return 64 + __builtin_ctzll(x>>64);
115115
};
116116

@@ -169,7 +169,7 @@ namespace Factor {
169169
if (x%2 == 0) return 2;
170170
if (x%3 == 0) return 3;
171171

172-
uniform_int_distribution<u64> rng(2, u64(x)-1);
172+
uniform_int_distribution<u64> rng(2, HI(x)? u64(-1ull): (u64(x)-1));
173173

174174
for (u128 i = 2; i < num_tries; i++) {
175175
u128 ans = rho(rng(rd), x, i);
@@ -244,4 +244,4 @@ int main()
244244
printf("^");
245245
write(cnt, '\n');
246246
}
247-
}
247+
}

0 commit comments

Comments
 (0)