結果

問題 No.3038 フィボナッチ数列の周期
ユーザー PachicobuePachicobue
提出日時 2018-04-01 23:40:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,052 bytes
コンパイル時間 2,121 ms
コンパイル使用メモリ 204,124 KB
実行使用メモリ 81,392 KB
最終ジャッジ日時 2023-09-08 12:59:10
合計ジャッジ時間 12,694 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
81,232 KB
testcase_01 AC 120 ms
81,000 KB
testcase_02 AC 77 ms
81,116 KB
testcase_03 AC 78 ms
81,044 KB
testcase_04 AC 78 ms
81,168 KB
testcase_05 AC 78 ms
81,044 KB
testcase_06 AC 76 ms
81,168 KB
testcase_07 AC 534 ms
81,204 KB
testcase_08 AC 436 ms
80,996 KB
testcase_09 AC 328 ms
81,032 KB
testcase_10 AC 587 ms
81,112 KB
testcase_11 AC 304 ms
81,100 KB
testcase_12 AC 472 ms
81,360 KB
testcase_13 AC 420 ms
81,000 KB
testcase_14 AC 509 ms
81,152 KB
testcase_15 AC 967 ms
81,392 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);
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            cerr << ans[i][j] << " ";
        }
        cerr << endl;
    }
    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 (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