import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum L = 10L^^8; long calc(const(long) X, const(long) Y) { long ret; /* 0 < j < i <= L j = 2 k X / Y = k / i */ if (Y > 2 * X) { ret += L / Y; } /* 0 < j < i <= L j = 2 k + 1 X / Y = (2 k (k + 1)) / (i (2 k + 1)) X > 0 ==> k > 0 */ void check(long d) { if (d >= 3 && d % 2 != 0) { const k = (d - 1) / 2; if ((2 * k * (k + 1)) % X == 0) { const q = (2 * k * (k + 1)) / X; if (Y / d <= L / q) { const i = (Y / d) * q; debug { writefln("X/Y = %s/%s, d = %s, k = %s, i = %s", X, Y, d, k, i); } if (2 * k + 1 < i && i <= L) { ++ret; } } } } } for (long d = 1; d^^2 <= Y; ++d) { if (Y % d == 0) { check(d); if (d != Y / d) { check(Y / d); } } } return ret; } void main() { debug { enum lim = 10; auto dp = new real[][](lim, lim); foreach (i; 0 .. lim) foreach (j; 0 .. lim) { if (i != j) { dp[i][j] = 0.0L; if (i > 0 && j > 0) { if (i > j) { dp[i][j] += (1.0L * (j - 1) / i) * dp[j - 1][j]; } else { dp[i][j] += (1.0L * (i - 1) / j) * dp[i][i - 1]; dp[i][j] += (1.0L * (j - (i - 1)) / j); } } } } foreach (i; 0 .. lim) { writeln(i, ": ", dp[i]); foreach (j; 1 .. i) { real prob = 1.0L * (j^^2 / 2) / (i * j); assert(abs(prob - dp[i][j]) <= 1e-12L); } if (i > 0) { foreach (j; i + 1 .. lim) { real prob = 1.0L - 1.0L * (i^^2 / 2) / (j * i); assert(abs(prob - dp[i][j]) <= 1e-12L); } } } } try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { const X = readLong(); const Y = readLong(); long ans; ans += calc(X, Y); ans += calc(Y - X, Y); writeln(ans); debug { foreach (i; 1 .. 100) { foreach (j; 1 .. i) { if (j % 2 != 0) { long p = j^^2 / 2; long q = i * j; const g = gcd(p, q); p /= g; q /= g; if (p == X && q == Y) { writeln("brt X/Y " , i, " ", j); } if (p == Y - X && q == Y) { writeln("brt 1-X/Y " , i, " ", j); } } } } } } } } catch (EOFException e) { } }