結果
問題 | No.8038 フィボナッチ数列の周期 |
ユーザー | Pachicobue |
提出日時 | 2018-04-01 23:41:36 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 682 ms / 3,000 ms |
コード長 | 3,096 bytes |
コンパイル時間 | 2,226 ms |
コンパイル使用メモリ | 199,572 KB |
最終ジャッジ日時 | 2025-01-05 09:47:31 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 25 |
ソースコード
#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 (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; }