#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> ms(n); std::vector idx(loop); for (int i = 0; i < loop; ++i) { int siz = ms[sm[i]].size(); ms[sm[i]].push_back(i); idx[i] = siz; } long long ans = 0; // r=m if (fib[m % loop] == 0) ++ans; // r=m-1 if (m > 0) { if (fib[(m - 1) % loop] == 0) ++ans; } if (m > 1) { 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 = ms[sm[rm]].size() - lc; int qmax = (m - rm - 1) / loop; 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); }