#include #include #include #include void 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 sm(w + 1); for (int i = 0; i < w; ++i) sm[i + 1] = (sm[i] + fib[i]) % n; assert(sm.back() == 0); std::vector num(n), idx(w); for (int i = 0; i < w; ++i) idx[i] = num[sm[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-2)+f(m-1)+f(m)+...+f(0) + 1 // r=m ans += (fm % n == 0); // r=m-1 ans += (m >= 1 and fm1 % n == 0); ans += (m >= 2 and (fm1 + fm2) % n == 0); // r<=m-2 for (int rm = 0; rm < w; ++rm) if (rm <= m - 1) { int lc = idx[rm], rc = num[sm[rm]] - lc; int qmax = (m - 1 - rm) / w; ans += 1LL * lc * (qmax + 1) * (qmax + 2) / 2; ans += 1LL * rc * (qmax + 0) * (qmax + 1) / 2; } std::cout << ans << '\n'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; std::cin >> n >> m; solve(n, m); }