真的没有想到会这么简单。
要使一个数 (p) 满足 条件, 则 存在(x, y), (a<=x imes p<=b && c<=y imes p <=d)
把(p) 除掉 则
(leftlceildfrac{a}{p} ight ceil <=y <=leftlfloordfrac{b}{p} ight floor)
(leftlceildfrac{c}{p} ight ceil <=y <=leftlfloordfrac{d}{p} ight floor)
把向上取整变为向下取整
(leftlfloordfrac{a+p-1}{p} ight floor <= leftlfloordfrac{b}{p} ight floor)
(leftlfloordfrac{b+p-1}{p} ight floor <= leftlfloordfrac{d}{p} ight floor)
然后就变成了 :
(leftlfloordfrac{a-1}{p} ight floor < leftlfloordfrac{b}{p} ight floor)
(leftlfloordfrac{b-1}{p} ight floor < leftlfloordfrac{d}{p} ight floor)
最后整除分块。 只需按照 (b/p)和(d/p) 相同时进行分类。 这样能使 (b/p) 和 (d/p)相等的同时 (c/p) 和 (d/p)尽量小, 更可能满足条件
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
#define R register
using namespace std;
inline int read() {
int X = 0, p = 1; char c = getchar();
for (; c > '9' || c < '0'; c = getchar())
if (c == '-') p = -1;
for (; c >= '0' && c <= '9'; c = getchar())
X = X * 10 + c - '0';
return X * p;
}
inline void cmax(int &A, int B) {
if (A < B) A = B;
}
inline int cmin(int A, int B) {
return A > B ? B : A;
}
void work() {
int ans = 1;
int a = rd - 1, b = rd, c = rd - 1, d = rd;
for (R int i = 1, j = 1, up = cmin(b, d); i <= up; i = j + 1) {
j = cmin(b / (b / i), d / (d / i));
if (b / j > a / j && d / j > c / j) cmax(ans, j);
}
printf("%d
", ans);
}
int main()
{
int n = rd;
for (; n; --n) work();
}