#include #include #include #include long long naive(int n, int m) { std::vector fib{ 1u, 1u }; for (int i = 2; i < m; ++i) { fib.push_back(fib[i - 1] + fib[i - 2]); } fib.resize(m); long long ans = 0; for (int r = m - 1; r >= 0; --r) { unsigned long long sum = 0; for (int l = r; l >= 0; --l) { if (fib[l] > fib[m - 1] - sum) break; sum += fib[l]; ans += sum % n == 0; } } return ans; } long long solve(int n, int m) { --m; int w; std::vector fib{ 1 % n }; for (int i = 1, x = 1 % n, y = 1 % n; ; ++i) { if (x == 1 % n and y == 0) { w = i; break; } fib.push_back(x); y = (x + y) % n; std::swap(x, y); } std::vector sum(w + 1); for (int i = 0; i < w; ++i) sum[i + 1] = (sum[i] + fib[i]) % n; assert(sum.back() == 0); std::vector num(n), idx(w); for (int i = 0; i < w; ++i) idx[i] = num[sum[i]]++; long long ans = 0; int fm = fib[m % w]; int fm1 = fib[(m + w - 1) % w]; int fm2 = fib[(m + w - 2) % w]; // f(m) = f(m) // f(m) = f(m-1)+f(m-2) // f(m) = f(m-2)+f(m-3)+...+f(0) + f(1) // r=m ans += (fm == 0); // r=m-1 ans += (m >= 1 and fm1 == 0); // ans += (m >= 2 and (fm1 + fm2) % n == 0); // r<=m-2 for (int rm = 0; rm < w; ++rm) if (rm <= m - 1) { long long lc = idx[rm], rc = num[sum[rm]] - lc; long long qmax = (m - 1 - rm) / w; ans += 1LL * lc * (qmax + 1) * (qmax + 2) / 2; ans += 1LL * rc * (qmax + 0) * (qmax + 1) / 2; } return ans; } #include int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::mt19937 rng{ std::random_device{}() }; // for (int k = 0; k < 10000; ++k) { // int n = 200000 - k % 100; // long long m = 2000000000 - k / 100; // std::cout << n << ' ' << m << ':' << solve(n, m) << std::endl; // } // for (int n = 198000; n >= 40000; --n) { // for (int m = 93; m <= 93; ++m) { // if (solve(n, m) != naive(n, m)) { // std::cerr << "Failed: " << n << ' ' << m << '\n'; // return 0; // } // break; // } // std::cerr << "Passed: " << n << '\n'; // } int n, m; std::cin >> n >> m; std::cout << solve(n, m) << '\n'; }