結果

問題 No.2929 Miracle Branch
ユーザー VvyLwVvyLw
提出日時 2024-10-14 22:12:16
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 4,078 bytes
コンパイル時間 2,370 ms
コンパイル使用メモリ 141,664 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-14 22:12:27
合計ジャッジ時間 10,198 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 2 ms
5,248 KB
testcase_17 AC 1 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 3 ms
5,248 KB
testcase_21 AC 3 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 4 ms
5,248 KB
testcase_24 AC 9 ms
5,248 KB
testcase_25 AC 3 ms
5,248 KB
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 2 ms
5,248 KB
testcase_28 AC 2 ms
5,248 KB
testcase_29 AC 2 ms
5,248 KB
testcase_30 AC 2 ms
5,248 KB
testcase_31 AC 2 ms
5,248 KB
testcase_32 AC 2 ms
5,248 KB
testcase_33 AC 2 ms
5,248 KB
testcase_34 AC 25 ms
5,248 KB
testcase_35 AC 23 ms
5,248 KB
testcase_36 AC 1 ms
5,248 KB
testcase_37 AC 24 ms
5,248 KB
testcase_38 AC 25 ms
5,248 KB
testcase_39 AC 26 ms
5,248 KB
testcase_40 AC 26 ms
5,248 KB
testcase_41 AC 25 ms
5,248 KB
testcase_42 AC 24 ms
5,248 KB
testcase_43 AC 27 ms
5,248 KB
testcase_44 AC 24 ms
5,248 KB
testcase_45 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#ifdef local
#include <C++/core/io/debug_print.hpp>
#else
#define dump(...) void(0);
#endif

#include <iostream>
#include <ranges>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cstdint>
namespace man {
constexpr uint LIM = 2e5;
bool miller(const uint64_t n);
namespace internal {
#ifndef TEMPLATE
typedef __uint128_t u128;
#endif
uint bsf(const uint64_t n){ return __builtin_ctzll(n); }
uint64_t gcd(uint64_t a, uint64_t b) {
    if(a == 0) {
        return b;
    }
    if(b == 0) {
        return a;
    }
    const uint shift = internal::bsf(a | b);
    a >>= internal::bsf(a);
    do {
        b >>= internal::bsf(b);
        if(a > b) {
            std::swap(a, b);
        }
        b -= a;
    } while(b > 0);
    return a << shift;
}
uint64_t mod_pow(const uint64_t a, uint64_t b, const uint64_t mod) {
    uint64_t r = 1;
    u128 x = a % mod;
    while(b > 0) {
        if(b & 1) {
            r = (u128(r) * x) % mod;
        }
        x = (u128(x) * x) % mod;
        b >>= 1;
    }
    return r;
}
uint64_t find(const uint64_t n) {
    if(miller(n)) {
        return n;
    }
    if(n % 2 == 0) {
        return 2;
    }
    int st = 0;
    const auto f = [&](const uint64_t x) -> uint64_t { return (u128(x) * x + st) % n; };
    while(true) {
        st++;
        uint64_t x = st, y = f(x);
        while(true) {
            const uint64_t p = gcd(y - x + n, n);
            if(p == 0 || p == n) {
                break;
            }
            if(p != 1) {
                return p;
            }
            x = f(x);
            y = f(f(y));
        }
    }
}
}
bool miller(const uint64_t n) {
    if(n <= 1) {
        return false;
    }
    if(n == 2) {
        return true;
    }
    if(n % 2 == 0) {
        return false;
    }
    uint64_t d = n - 1;
    while(d % 2 == 0) {
        d /= 2;
    }
    for(const uint a: {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
        if(n <= a) {
            break;
        }
        uint64_t t = d, y = internal::mod_pow(a, t, n);
        while(t != n - 1 && y != 1 && y != n - 1) {
            y = internal::mod_pow(y, 2, n);
            t <<= 1;
        }
        if(y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
std::vector<uint64_t> rho(const uint64_t n) {
    if(n == 1) {
        return {};
    }
    const uint64_t x = internal::find(n);
    if(x == n) {
        return {x};
    }
    std::vector<uint64_t> le = rho(x);
    const std::vector<uint64_t> ri = rho(n / x);
    le.insert(le.end(), ri.begin(), ri.end());
    return le;
}
/**
 * @brief Pollard's Rho
 * @docs docs/pollard_rho.md
 */
}
int main() {
    std::cin.tie(nullptr) -> sync_with_stdio(false);
    using namespace std::views;
    int64_t x;
    std::cin >> x;
    if(x == 1) {
        std::cout << "2\n1 2\nb g\n";
        std::exit(0);
    }
    auto pf = man::rho(x);
    while(std::ssize(pf) > 1) {
        std::ranges::sort(pf, std::greater<>());
        const auto l = pf.back();
        pf.pop_back();
        const auto r = pf.back();
        if(l * r <= l + r + 1) {
            pf.pop_back();
            pf.emplace_back(l * r);
        } else {
            pf.emplace_back(l);
            break;
        }
    }
    dump(pf);
    const int n = std::ssize(pf);
    const auto ret = std::accumulate(pf.cbegin(), pf.cend(), 0L) + n;
    if(ret > man::LIM) {
        std::cout << "-1\n";
        std::exit(0);
    }
    std::cout << ret << '\n';
    for(const auto i: iota(1) | take(n - 1)) {
        std::cout << i << ' ' << i + 1 << '\n';
    }
    int k = n;
    for(const auto i: iota(0, n)) {
        for([[maybe_unused]] const auto _: iota(0U, pf[i])) {
            std::cout << i + 1 << ' ' << ++k << '\n';
        }
    }
    for([[maybe_unused]] const auto _: iota(0, n)) {
        std::cout << "b ";
    }
    for(const auto i: iota(0, ret - n)) {
        std::cout << 'g' << " \n"[i + 1 == ret - n];
    }
}
0