#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; void testcase(){ i64 N, M; cin >> N >> M; vector F = {1,1}; for(i64 i=2; ; i++){ if(F[i-1] == 1 && F[i-2] == 0) break; F.push_back((F[i-1] + F[i-2]) % N); } i64 C = F.size(); auto fib = [&](i64 n) -> i64 { return F[n%C]; }; vector> A; i64 ans = 0; if(fib(M) == 0) ans += 1; if(M >= 2 && fib(M-1) == 0) ans += 1; if(M >= 3 && fib(M) == 0) ans += 1; if(M >= 3){ rep(i,C) A.push_back({ fib(i+1), ((M-1) + (C-1) - i) / C }); i64 q = 0; for(auto [x,y] : A) q += y; sort(A.begin(), A.end()); A.push_back({-2,0}); i64 a = -1, c = 0; for(auto [x,y] : A){ if(a != x){ ans += c * (c-1) / 2; a = x; c = 0; } c += y; } } cout << ans << '\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }