結果

問題 No.3038 フィボナッチ数列の周期
ユーザー PachicobuePachicobue
提出日時 2018-04-02 11:44:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 206 ms / 3,000 ms
コード長 2,948 bytes
コンパイル時間 2,733 ms
コンパイル使用メモリ 206,448 KB
実行使用メモリ 81,528 KB
最終ジャッジ日時 2024-06-26 06:32:38
合計ジャッジ時間 6,871 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
81,492 KB
testcase_01 AC 81 ms
81,528 KB
testcase_02 AC 83 ms
81,524 KB
testcase_03 AC 81 ms
81,424 KB
testcase_04 AC 80 ms
81,288 KB
testcase_05 AC 81 ms
81,428 KB
testcase_06 AC 81 ms
81,464 KB
testcase_07 AC 82 ms
81,432 KB
testcase_08 AC 83 ms
81,412 KB
testcase_09 AC 81 ms
81,356 KB
testcase_10 AC 113 ms
81,316 KB
testcase_11 AC 76 ms
81,344 KB
testcase_12 AC 78 ms
81,356 KB
testcase_13 AC 77 ms
81,380 KB
testcase_14 AC 76 ms
81,408 KB
testcase_15 AC 78 ms
81,420 KB
testcase_16 AC 202 ms
81,320 KB
testcase_17 AC 199 ms
81,468 KB
testcase_18 AC 206 ms
81,308 KB
testcase_19 AC 201 ms
81,384 KB
testcase_20 AC 200 ms
81,396 KB
testcase_21 AC 203 ms
81,500 KB
testcase_22 AC 201 ms
81,396 KB
testcase_23 AC 201 ms
81,324 KB
testcase_24 AC 199 ms
81,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
ll power(const ll p, const ll k)
{
    if (k == 0) return 1;
    if (k % 2 == 1) {
        return power(p, k - 1) * p % MOD;
    } else {
        const ll pp = power(p, k / 2);
        return pp * pp % MOD;
    }
}

constexpr int MAX = 10000000;
constexpr int SQRT = 3163;
bool isprime[SQRT];

array<array<ll, 2>, 2> mul(const array<array<ll, 2>, 2>& m1, const array<array<ll, 2>, 2>& m2, const ll mod)
{
    array<array<ll, 2>, 2> ans{{{0, 0}, {0, 0}}};
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                (ans[i][j] += m1[i][k] * m2[k][j] % mod) %= mod;
            }
        }
    }
    return ans;
}

array<array<ll, 2>, 2> power(const array<array<ll, 2>, 2>& m, const ll n, const ll mod)
{
    if (n == 0) return array<array<ll, 2>, 2>{{{1, 0}, {0, 1}}};
    if (n % 2 == 1) {
        return mul(power(m, n - 1, mod), m, mod);
    } else {
        const auto pp = power(m, n / 2, mod);
        return mul(pp, pp, mod);
    }
}

bool isperiod(const ll n, const ll p)
{
    const array<array<ll, 2>, 2> id{{{1, 0}, {0, 1}}};
    const array<array<ll, 2>, 2> mat{{{1, 1}, {1, 0}}};
    const auto ans = power(mat, n, p);
    return ans == id;
}

int main()
{
    fill(isprime, isprime + SQRT, true);
    vector<ll> prime;
    for (int i = 2; i < SQRT; i++) {
        if (not isprime[i]) continue;
        prime.push_back(i);
        for (int j = 2; i * j < SQRT; j++) {
            isprime[i * j] = false;
        }
    }
    int N;
    cin >> N;
    vector<ll> ind(MAX, 0);
    for (int i = 0; i < N; i++) {
        ll p, k;
        cin >> p >> k;
        if (p == 2) {
            ind[2] = max(ind[2], k - 1);
            ind[3] = max(ind[3], 1LL);
        } else if (p == 5) {
            ind[2] = max(ind[2], 2LL);
            ind[5] = max(ind[5], k);
        } else {
            const ll PER = (p % 5 == 1 or p % 5 == 4) ? p - 1 : 2 * p + 2;
            ll per = PER;
            for (ll d = 2; d * d <= PER; d++) {
                if (PER % d != 0) continue;
                if (isperiod(d, p)) {
                    per = min(per, d);
                }
                if (d * d != per and isperiod(PER / d, p)) {
                    per = min(per, PER / d);
                }
            }

            for (const ll P : prime) {
                if (per % P != 0) continue;
                ll num = 0;
                while (per % P == 0) {
                    per /= P;
                    num++;
                }
                ind[P] = max(ind[P], num);
            }
            ind[p] = max(ind[p], k - 1);
            if (per != 1) {
                ind[per] = max(ind[per], 1LL);
            }
        }
    }
    ll ans = 1;
    for (int i = 0; i < MAX; i++) {
        (ans *= power(i, ind[i])) %= MOD;
    }
    cout << ans << endl;
    return 0;
}
0