/
Factor.h
62 lines (60 loc) · 2.35 KB
/
Factor.h
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
/**
* Author: chilli, SJTU, pajenegod
* Date: 2020-03-04
* License: CC0
* Source: own
* Description: Pollard-rho randomized factorization algorithm. Returns prime
* factors of a number, in arbitrary order (e.g. 2299 -> \{11, 19, 11\}).
* Time: $O(n^{1/4})$, less for numbers with small factors.
* Status: stress-tested
*
* Details: This implementation uses the improvement described here
* (https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm#Variants), where
* one can accumulate gcd calls by some factor (40 chosen here through
* exhaustive testing). This improves performance by approximately 6-10x
* depending on the inputs and speed of gcd. Benchmark found here:
* (https://ideone.com/nGGD9T)
*
* GCD can be improved by a factor of 1.75x using Binary GCD
* (https://lemire.me/blog/2013/12/26/fastest-way-to-compute-the-greatest-common-divisor/).
* However, with the gcd accumulation the bottleneck moves from the gcd calls
* to the modmul. As GCD only constitutes ~12% of runtime, speeding it up
* doesn't matter so much.
*
* This code can probably be sped up by using a faster mod mul - potentially
* montgomery reduction on 128 bit integers.
* Alternatively, one can use a quadratic sieve for an asymptotic improvement,
* which starts being faster in practice around 1e13.
*
* Brent's cycle finding algorithm was tested, but doesn't reduce modmul calls
* significantly.
*
* Subtle implementation notes:
* - we operate on residues in [1, n]; modmul can be proven to work for those
* - prd starts off as 2 to handle the case n = 4; it's harmless for other n
* since we're guaranteed that n > 2. (Pollard rho has problems with prime
* powers in general, but all larger ones happen to work.)
* - t starts off as 30 to make the first gcd check come earlier, as an
* optimization for small numbers.
*/
#pragma once
#include "ModMulLL.h"
#include "MillerRabin.h"
ull pollard(ull n) {
auto f = [n](ull x) { return modmul(x, x, n) + 1; };
ull x = 0, y = 0, t = 30, prd = 2, i = 1, q;
while (t++ % 40 || __gcd(prd, n) == 1) {
if (x == y) x = ++i, y = f(x);
if ((q = modmul(prd, max(x,y) - min(x,y), n))) prd = q;
x = f(x), y = f(f(y));
}
return __gcd(prd, n);
}
vector<ull> factor(ull n) {
if (n == 1) return {};
if (isPrime(n)) return {n};
ull x = pollard(n);
auto l = factor(x), r = factor(n / x);
l.insert(l.end(), all(r));
return l;
}