求
a<=x<=b,c<=y<=d,gcd(x,y)
的最大值。
我们考虑枚举g=1~min(b,d),检验是否合法即是否满足
⌊a−1g⌋<⌊bg⌋
&&
⌊c−1g⌋<⌊dg⌋
我们发现 ⌊bg⌋ 这个东西的取值是 b√ 的,因此我们分块枚举即可,此时last一定是最优的。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 1000010
inline char gc(){
static char buf[1<<16],*S,*T;
if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
int main(){
// freopen("a.in","r",stdin);
int n=read();
while(n--){
int a=read(),b=read(),c=read(),d=read(),ans=0;
for(int i=1,last;i<=b&&i<=d;i=last+1){
last=min(b/(b/i),d/(d/i));
if(b/last>(a-1)/last&&d/last>(c-1)/last) ans=last;
}printf("%d\n",ans);
}return 0;
}