Pollard-rho

· · 个人记录

#include <bits/stdc++.h>

using namespace std;

namespace miller_rabin {
    unsigned __int128 quick_pow(unsigned __int128 a, unsigned __int128 b, unsigned __int128 p) {
        unsigned __int128 res = 1;
        while (b) {
            if (b & 1) {
                res *= a;
                res %= p;
            }
            a *= a;
            a %= p;
            b >>= 1;
        }
        return res;
    }

    vector<unsigned __int128> test_set = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};

    bool miller_rabin_primality_test(unsigned __int128 x) {
        if (x == 2) {
            return true;
        } else if (x <= 1 || x % 2 == 0) {
            return false;
        } else {
            unsigned __int128 tmp;
            for (auto i: test_set) {
                if (x > i && x % i == 0) {
                    return false;
                } else if (i % x != 0) {
                    i %= x;
                    unsigned __int128 u = x - 1, t = 0;
                    while (u % 2 == 0) {
                        t++;
                        u >>= 1;
                    }
                    tmp = quick_pow(i, u, x);
//        cout << i << " " << t << " " << u << "\n";
                    for (int j = 0; j < t; ++j) {
                        unsigned __int128 b = tmp * tmp % x;
                        if (b == 1 && tmp != x - 1 && tmp != 1) {
//                cout << j << " " << b << " " << tmp << "\n";
                            return false;
                        } else {
                            tmp = b;
                        }
                    }
                    if (tmp != 1) {
                        return false;
                    }
                }
            }
            return true;
        }
    }
}

namespace pollard_rho {
    mt19937_64 rnd(random_device{}());

    inline unsigned __int128 f(unsigned __int128 x, unsigned __int128 y) {
        if (x > y) {
            return x - y;
        } else {
            return y - x;
        }
    }

    list<unsigned __int128> fact(unsigned __int128 n) {
       if (miller_rabin::miller_rabin_primality_test(n)) {
            return {n};
        } else if (n < 10) {
            if (n == 0 || n == 1) {
                return {};
            } else {
                switch (n) {
                    case 4:
                        return {2, 2};
                    case 6:
                        return {2, 3};
                    case 8:
                        return {2, 2, 2};
                    case 9:
                        return {3, 3};
                }
            }
        } else {
            int cnt = 0;
            while (cnt < 1000) {
                const unsigned __int128 c = rnd() % (n - 1) + 1;
                unsigned __int128 x = c, y = (c * c + c) % n;
                for (; x != y; x = (x * x + c) % n, y = (y * y + c) % n, y = (y * y + c) % n) {
                    unsigned __int128 p = __gcd(f(x, y), n);
                    if (p == n) {
                        break;
                    } else if (p > 1 && p < n) {
                        auto res = fact(p), tmp = fact(n / p);
                        res.splice(res.end(), tmp);
                        return res;
                    }
                }
                cnt++;
            }
            return {n};
        }
    }
}

unsigned long long n;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    auto ans = pollard_rho::fact(n);
    ans.sort();
    for (auto i: ans) {
        cout << (unsigned long long) i << " ";
    }
    return 0;
}