#include #include #include #include long long naive(int n, int m) { std::vector fib{ 1, 1 }; 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) { 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; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); // for (int n = 1; n <= 10000; ++n) { // for (int m = 1; m <= 90; ++m) { // if (solve(n, m) != naive(n, m)) { // std::cerr << "Failed: " << n << ' ' << m << '\n'; // return 0; // } // } // std::cerr << "Passed: " << n << '\n'; // } int n, m; std::cin >> n >> m; std::cout << solve(n, m) << '\n'; }