u see this problem F. Kosuke's Sloth time limit per test1 second memory limit per…
- User
u see this problem F. Kosuke's Sloth time limit per test1 second memory limit per test256 megabytes Kosuke is too lazy. He will not give you any legend, just the task:
Fibonacci numbers are defined as follows:
f(1)=f(2)=1 . f(n)=f(n−1)+f(n−2) (3≤n) We denote G(n,k) as an index of the n -th Fibonacci number that is divisible by k . For given n and k , compute G(n,k) . As this number can be too big, output it by modulo 109+7 .
For example: G(3,2)=9 because the 3 -rd Fibonacci number that is divisible by 2 is 34 . [1,1,2,3,5,8,13,21,34] .
Input The first line of the input data contains a single integer t (1≤t≤104 ) — the number of test cases.
The first and only line contains two integers n and k (1≤n≤1018 , 1≤k≤105 ).
It is guaranteed that the sum of k across all test cases does not exceed 106 .
Output For each test case, output the only number: the value G(n,k) taken by modulo 109+7 .
Example InputCopy 3 3 2 100 1 1000000000000 1377 OutputCopy 9 100 999244007
this is the solution, pls explain it to me, first time to hear about Pisano period: #include <bits/stdc++.h> #define int long long using namespace std;
const int MOD = 1e9 + 7;
signed main() { cin.tie(0); ios::sync_with_stdio(false); int t; cin >> t; int k_max = 100000; vector<vector> z(k_max + 1); vector L(k_max + 1, 0); while (t--) { int n, k; cin >> n >> k; if (k == 1) { cout << n % MOD << "\n"; continue; } if (L[k] == 0) { int p = 0, c = 1; vector tmp; int prev = 0, curr = 1; for (int i = 1; i <= 6 * k; i++) { if (curr % k == 0) { tmp.push_back(i); } int temp = (prev + curr) % k; prev = curr; curr = temp; if (prev == 0 && curr == 1) { p = i; break; } } if (p == 0)p = 6 * k; L[k] = p; z[k] = tmp; } int period = L[k]; int C = z[k].size(); if (C == 0) { cout << -1 << "\n"; continue; } int full = n / C; int rem = n % C; int res = 0; if (rem == 0) { res = (full % MOD * period % MOD) % MOD; } else { res = (full % MOD * period % MOD) % MOD; res = (res + z[k][rem - 1]) % MOD; } cout << res << "\n"; } }
- Assistant