#include #include #include #include void solve(int n, int m) { --m; std::map, int> memo; memo[{1 % n, 0}] = 0; int loop; std::vector fib{ 1 % n }; for (int i = 1, x = 1 % n, y = 1 % n; ; ++i) { auto [it, inserted] = memo.try_emplace(std::make_pair(x, y), i); if (not inserted) { assert(it->second == 0); loop = i - it->second; break; } fib.push_back(x); y = (x + y) % n; std::swap(x, y); } std::vector sm(loop + 1); for (int i = 0; i < loop; ++i) { sm[i + 1] = (sm[i] + fib[i]) % n; } assert(sm.back() == 0); std::vector mod_count(n); std::vector idx(loop); for (int i = 0; i < loop; ++i) { idx[i] = mod_count[sm[i]]++; } long long ans = 0; // r=m if (fib[m % loop] == 0) ++ans; // r=m-1 if (m >= 1) { if (fib[(m - 1) % loop] == 0) ++ans; } if (m >= 2) { if ((fib[(m - 2) % loop] + fib[(m - 1) % loop]) % n == 0) ++ans; } // r<=m-2 for (int rm = 0; rm < loop; ++rm) { if (rm > m - 1) break; int lc = idx[rm], rc = mod_count[sm[rm]] - lc; int qmax = (m - rm - 1) / loop; ans += 1LL * lc * (qmax + 1) * (qmax + 2) / 2; ans += 1LL * rc * (qmax + 0) * (qmax + 1) / 2; } // l=1, r=m-2 if ((fib[m % loop] + 1) % n == 0) --ans; 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); }