File tree Expand file tree Collapse file tree 2 files changed +10
-8
lines changed Expand file tree Collapse file tree 2 files changed +10
-8
lines changed Original file line number Diff line number Diff line change 2
2
#define CP_ALGO_MATH_FACTORIZE_HPP
3
3
#include " primality.hpp"
4
4
#include " ../random/rng.hpp"
5
+ #include < generator>
5
6
namespace cp_algo ::math {
6
7
// https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm
7
- std::basic_string <uint64_t > factorize (uint64_t m) {
8
+ std::generator <uint64_t > factorize (uint64_t m) {
8
9
if (m % 2 == 0 ) {
9
- return factorize (m / 2 ) + (uint64_t )2 ;
10
+ co_yield std::ranges::elements_of (factorize (m / 2 ));
11
+ co_yield 2 ;
10
12
} else if (is_prime (m)) {
11
- return {m} ;
13
+ co_yield m ;
12
14
} else if (m > 1 ) {
13
15
using base = dynamic_modint<int64_t >;
14
- return base::with_mod (m, [&]() {
16
+ auto g = base::with_mod (m, [&]() {
15
17
base t = random::rng ();
16
18
auto f = [&](auto x) {
17
19
return x * x + t;
@@ -32,10 +34,10 @@ namespace cp_algo::math {
32
34
}
33
35
g = std::gcd (g.getr (), m);
34
36
}
35
- return factorize ( g.getr ()) + factorize (m / g. getr () );
37
+ return g.getr ();
36
38
});
37
- } else {
38
- return {} ;
39
+ co_yield std::ranges::elements_of ( factorize (g));
40
+ co_yield std::ranges::elements_of ( factorize (m / g)) ;
39
41
}
40
42
}
41
43
}
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ using namespace cp_algo::math;
10
10
void solve () {
11
11
int64_t m;
12
12
cin >> m;
13
- auto res = factorize (m);
13
+ auto res = to<vector>( factorize (m) );
14
14
ranges::sort (res);
15
15
cout << size (res) << " " ;
16
16
ranges::copy (res, ostream_iterator<int64_t >(cout, " " ));
You can’t perform that action at this time.
0 commit comments