結果

問題 No.8038 フィボナッチ数列の周期
コンテスト
ユーザー himueu
提出日時 2026-11-28 21:54:41
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 8 ms / 3,000 ms
+ 661µs
コード長 9,413 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,962 ms
コンパイル使用メモリ 213,312 KB
実行使用メモリ 9,876 KB
最終ジャッジ日時 2026-11-28 21:54:50
合計ジャッジ時間 4,015 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <atcoder/modint>
#include <algorithm>
#include <bit>
#include <cassert>
#include <concepts>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <unordered_map>
#include <utility>
#include <vector>

template<class U0, class U1>
struct Montgomery {
    static_assert(sizeof(U0) * 2 == sizeof(U1));
    constexpr static unsigned B0 = sizeof(U0) * 8U;
    U0 n, nr, rs, np;
    
    constexpr Montgomery(const U0 &Mod) {
        SetMod(Mod);
    }
    
    constexpr U0 GetMod() const noexcept {
        return n;
    }
    constexpr void SetMod(const U0 &Mod) {
        assert(Mod >= 2);
        assert(Mod % 2 == 1);
        assert(Mod >> (B0 - 2) == 0);
        n = nr = Mod;
        rs = -static_cast<U1>(n) % n;
        for (uint32_t i = 0; i < std::bit_width(B0) - 1; i++) {
            nr *= 2 - n * nr;
        }
        np = reduce(static_cast<U0>(1), rs);
    }
    constexpr U0 reduce(const U0 &x) const noexcept {
        const U0 q = x * nr;
        const U0 m = (static_cast<U1>(q) * n) >> B0;
        const U0 res = n - m;
        return (res >= n) ? res - n : res;
    }
    constexpr U0 reduce(const U0 &x, const U0 &y) const noexcept {
        const U1 t = static_cast<U1>(x) * y;
        const U0 c = t, d = t >> B0;
        const U0 q = c * nr;
        const U0 m = (static_cast<U1>(q) * n) >> B0;
        const U0 res = d + n - m;
        return (res >= n) ? res - n : res;
    }
    constexpr U0 reduce(const U0 &x, const U0 &y, const U0 &z) const noexcept {
        const U1 t = static_cast<U1>(x) * y;
        const U0 c = t, d = t >> B0;
        const U0 q = c * nr;
        const U0 m = (static_cast<U1>(q) * n) >> B0;
        U0 res = d + n - m;
        if (res >= n) { res -= n; }
        res += z;
        if (res >= n) { res -= n; }
        return res;
    }
    constexpr U0 val(const U0 &x) const noexcept {
        return reduce(x);
    }
    constexpr U0 zero() const noexcept {
        return static_cast<U0>(0);
    }
    constexpr U0 one() const noexcept {
        return np;
    }
    constexpr U0 raw(const U0 &x) const noexcept {
        return reduce(x, rs);
    }
    template<class U> requires std::unsigned_integral<U>
    constexpr U0 trans(const U &x) const noexcept {
        if (x < n) {
            return raw(x);
        }
        return reduce(x % n, rs);
    }
    template<class S> requires std::signed_integral<S>
    constexpr U0 trans(S x) const noexcept {
        if (0 <= x && x < static_cast<S>(n)) {
            return raw(x);
        }
        x %= static_cast<S>(n);
        if (x < 0) {
            x += static_cast<S>(n);
        }
        return reduce(x, rs);
    }
    constexpr U0 neg(const U0 &x) const noexcept {
        return (x == 0) ? 0 : (n - x);
    }
    constexpr U0 inc(const U0 &x) const noexcept {
        return add(x, np);
    }
    constexpr U0 dec(const U0 &x) const noexcept {
        return sub(x, np);
    }
    constexpr U0 add(const U0 &x, const U0 &y) const noexcept {
        const U0 z = x + y;
        return (z >= n) ? z - n : z;
    }
    constexpr U0 sub(const U0 &x, const U0 &y) const noexcept {
        return (x < y) ? (x - y + n) : (x - y);
    }
    constexpr U0 mul(const U0 &x, const U0 &y) const noexcept {
        return reduce(x, y);
    }
    constexpr U0 mul_add(const U0 &x, const U0 &y, const U0 &z) const noexcept {
        return reduce(x, y, z);
    }
    constexpr U0 pow(const U0 &x, U0 k) const noexcept {
        U0 res = np;
        U0 b = x;
        while (k > 0) {
            if (k & 1) { res = mul(res, b); }
            b = mul(b, b);
            k >>= 1;
        }
        return res;
    }
    constexpr bool same(const U0 &x, const U0 &y) const noexcept {
        return x == y;
    }
};

bool is_prime(uint64_t n) {
    using u64 = uint64_t;
    using u128 = __uint128_t;
    if (n < 2) { return false; }
    for (u64 p : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
        if (n % p == 0) { return n == p; }
    }
    if (n < 41 * 41) { return true; }

    Montgomery<u64, u128> mont(n);
    const u64 one = mont.one(), neg_one = mont.neg(one);
    auto miller_rabin = [&](const std::vector<u64> &bases) -> bool {
        u64 d = n - 1;
        int e = 0;
        while (~d & 1) {
            d >>= 1;
            e++;
        }
        for (u64 b : bases) {
            u64 x = mont.pow(mont.trans(b), d);
            if (x == one) { continue; }
            for (int i = 0; i < e; i++) {
                u64 y = mont.mul(x, x);
                if (y == one) {
                    if (x == neg_one) { break; }
                    return false;
                }
                x = y;
                if (i == e - 1) { return false; }
            }
        }
        return true;
    };

    if (n < 2047) { return miller_rabin({2}); }
    if (n < 9080191) { return miller_rabin({31, 73}); }
    if (n < 4759123141) { return miller_rabin({2, 7, 61}); }
    if (n < 1122004669633) { return miller_rabin({2, 13, 23, 1662803}); }
    if (n < 3770579582154547) { return miller_rabin({2, 880937, 2570940, 610386380, 4130785767}); }
    return miller_rabin({2, 325, 9375, 28178, 450775, 9780504, 1795265022});
}

std::vector<std::pair<uint64_t, uint32_t>> factorize(uint64_t n) {
    using namespace std;
    using u32 = uint32_t;
    using u64 = uint64_t;
    using u128 = __uint128_t;
    vector<pair<u64, u32>> res;
    for (u64 p : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}) {
        u32 cnt = 0;
        while (n % p == 0) {
            cnt++;
            n /= p;
        }
        if (cnt > 0) { res.emplace_back(p, cnt); }
    }
    if (n == 1) { return res; }
    if (is_prime(n)) {
        res.emplace_back(n, 1);
        return res;
    }

    auto pollard_rho = [&](u64 n) -> u64 {
        if (n % 2 == 0) { return 2; }
        Montgomery<u64, u128> mont(n);
        const u64 one = mont.one(), two = mont.trans(2);
        constexpr u64 m = 1 << 8;

        u64 c = one;
        while (true) {
            auto f = [&](u64 x) { return mont.mul_add(x, x, c); };
            u64 x = two, y = two;
            while (true) {
                u64 q = one;
                vector<u64> diffs;
                diffs.reserve(m);
                for (u64 i = 0; i < m; i++) {
                    y = f(f(y));
                    x = f(x);
                    const u64 diff = mont.sub(x, y);
                    diffs.push_back(diff);
                    q = mont.mul(q, diff);
                }
                const u64 g = gcd(q, n);
                if (g == 1) { continue; }
                if (g != n) { return g; }

                for (u64 diff : diffs) {
                    const u64 h = gcd(diff, n);
                    if (h != 1 && h != n) { return h; }
                }
                c = mont.add(c, one);
                break;
            }
        }
    };

    auto update = [&](u64 p) -> void {
        for (auto &[q, cnt] : res) {
            if (p == q) {
                cnt++;
                return;
            }
        }
        res.emplace_back(p, 1);
    };

    auto dfs = [&](auto dfs, u64 x) -> void {
        if (x == 1) { return; }
        if (is_prime(x)) {
            update(x);
            return;
        }
        const u64 d = pollard_rho(x);
        dfs(dfs, d);
        dfs(dfs, x / d);
    };
    dfs(dfs, n);

    sort(res.begin(), res.end());
    return res;
}

uint64_t fibonacci(uint64_t n, uint64_t p) {
    assert(p > 1);
    using mint = atcoder::modint;
    mint::set_mod(p);
    using Mat = std::array<std::array<mint, 2>, 2>;
    auto mat_mul = [&](const Mat x, const Mat y) -> Mat {
        Mat res{};
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) { res[i][j] += x[i][k] * y[k][j]; }
            }
        }
        return res;
    };
    auto mat_pow = [&](Mat x, uint64_t k) -> Mat {
        Mat res{{{1, 0}, {0, 1}}};
        while (k > 0) {
            if (k & 1) { res = mat_mul(res, x); }
            x = mat_mul(x, x);
            k /= 2;
        }
        return res;
    };
    Mat x{{{0, 1}, {1, 1}}};
    Mat res = mat_pow(x, n);
    return res[0][1].val();
}

uint64_t pisano_p(uint64_t p) {
    if (p == 2) { return 3; }
    if (p == 5) { return 20; }
    uint64_t res = (p % 5 == 1 || p % 5 == 4) ? p - 1 : p * 2 + 2;
    auto factors = factorize(res);
    for (auto [q, e] : factors) {
        while (res % q == 0) {
            uint64_t nres = res / q;
            if (fibonacci(nres, p) == 0 && fibonacci(nres + 1, p) == 1) {
                res = nres;
            } else {
                break;
            }
        }
    }
    return res;
}

std::unordered_map<uint64_t, uint32_t> pisano_pow_p(uint64_t p, uint32_t k) {
    std::unordered_map<uint64_t, uint32_t> res;
    res[p] = k - 1;
    for (auto [q, e] : factorize(pisano_p(p))) { res[q] += e; }
    return res;
}

uint64_t pisano() {
    std::unordered_map<uint64_t, uint32_t> mp;
    uint32_t n;
    std::cin >> n;
    while (n--) {
        uint64_t p;
        uint32_t k;
        std::cin >> p >> k;
        for (auto [q, e] : pisano_pow_p(p, k)) { mp[q] = std::max(mp[q], e); }
    }
    using mint = atcoder::modint1000000007;
    mint res = 1;
    for (auto [q, e] : mp) { res *= mint(q).pow(e); }
    return res.val();
}

int main() {
    using namespace std;
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << pisano() << '\n';
}
0