#pragma GCC optimize("Ofast") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; /* ポラードのρ法 参考資料 - https://qiita.com/Kiri8128/items/eca965fe86ea5f4cbb98 - https://manabitimes.jp/math/1192 */ // verify:https://www.acmicpc.net/problem/4149 namespace factorize{ using u64 = uint64_t; using u128 = __uint128_t; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); u64 binary_gcd(u64 a, u64 b) { if (a == 0) return b; if (b == 0) return a; const int n = __builtin_ctzll(a | b); a >>= __builtin_ctzll(a); while (b > 0) { b >>= __builtin_ctzll(b); if (a > b) std::swap(a, b); b -= a; } return a << n; } u128 pow (u128 a, u64 n, u128 mod) { u128 res = 1; if (a >= mod) a %= mod; while (n > 0) { if (n & 1) { res *= a; if (res >= mod) res %= mod; } a *= a; if (a >= mod) a %= mod; n >>= 1; } return res; } bool miller_rabin (u64 n, vector v) { u64 d = n-1; while (~d & 1) d >>= 1; for (u64 a:v) { if (n <= a) break; u64 t = d; u128 y = pow(a, t, n); while (t != n-1 and y != 1 and y != n-1) { y *= y; if(y >= n) y %= n; t *= 2; } if (y != n-1 and t % 2 == 0) return false; } return true; } bool is_prime (u64 n) { if (n <= 1) return false; if (~n & 1) return (n == 2); if (n < (1LL << 30)) { return miller_rabin(n, {2, 7, 61}); } else { return miller_rabin(n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022}); } } template T pollard_rho (T n) { if (~n & 1) return 2; if (is_prime(n)) return n; static u128 x,y,c,d; auto f = [&](u128 x) {return (x * x % n + c) % n;}; auto rnd_ = [&](T l, T r) {return rng() % (r - l + 1) + l;}; x = rnd_(2, n); y = x; c = rnd_(1, n); d = 1; while (d == 1) { x = f(x); y = f(y); y = f(y); d = binary_gcd((x > y ? x-y : y-x), n); if ((T)d == n) { return pollard_rho(n); } } if (is_prime(d)) { return d; } else { return pollard_rho(d); } } template vector prime_factor (T n) { vector res; for (T i = 2; i*i <= n;) { while (n % i == 0) { n /= i; res.emplace_back(i); } i += 1 + (~n & 1); if (i >= 101 and n >= (1<<20)) { while (n > 1) { auto p = pollard_rho(n); while (n % p == 0) { n /= p; res.emplace_back(p); } } break; } } if (n > 1) res.emplace_back(n); sort(res.begin(), res.end()); return res; } template map factor_count (T n) { map mp; for (auto &x : prime_factor(n)) mp[x]++; return mp; } template vector divisors(T n) { if (n == 0) return {}; vector> v; for(auto &p : factor_count(n)) v.push_back(p); vector res; auto f = [&](auto self, int i, T x) -> void { if (i == (int)v.size()) { res.push_back(x); return; } for (int j = 0; j <= v[i].second; ++j) { self(self, i + 1, x); if (j+1 <= v[i].second) { x *= v[i].first; } } }; f(f, 0, 1); sort(res.begin(), res.end()); return res; } } // namespace factorize int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); ll p,q; cin >> p >> q; ll g = gcd(p,q); p /= g; q /= g; // MQ+NQ=PNM // PMQ+PNQ=P^2NM // (PN - Q)(PM - Q) = Q^2 auto d = factorize::divisors(q*q); vector> res; for(int i=0;i