結果

問題 No.3038 フィボナッチ数列の周期
ユーザー PachicobuePachicobue
提出日時 2018-04-01 23:24:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,994 bytes
コンパイル時間 2,239 ms
コンパイル使用メモリ 203,312 KB
実行使用メモリ 162,392 KB
最終ジャッジ日時 2023-09-08 12:55:21
合計ジャッジ時間 9,619 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 75 ms
81,120 KB
testcase_03 AC 75 ms
81,056 KB
testcase_04 AC 77 ms
80,952 KB
testcase_05 AC 76 ms
81,024 KB
testcase_06 AC 75 ms
81,144 KB
testcase_07 AC 152 ms
80,988 KB
testcase_08 AC 136 ms
81,024 KB
testcase_09 AC 116 ms
80,988 KB
testcase_10 AC 161 ms
80,964 KB
testcase_11 AC 112 ms
81,112 KB
testcase_12 AC 142 ms
81,028 KB
testcase_13 AC 135 ms
80,940 KB
testcase_14 AC 150 ms
81,140 KB
testcase_15 AC 229 ms
81,112 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

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);
            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 (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++) {
        // if (ind[i] > 0) {
        //     cerr << i << ": " << ind[i] << endl;
        // }
        (ans *= power(i, ind[i])) %= MOD;
    }
    cout << ans << endl;
    return 0;
}
0