Luogu P3579 [POI2014]PAN-Solar Panels
题目大意:
给出T组询问,每组询问给出四个数a,b,c,d,每次询问满足a<=x<=b,c<=y<=d的gcd(x,y)的最大值
首先可以想到如果存在gcd(x,y)=k,那么就一定要满足b/k>(a-1)/k&&d/k>(c-1)/k。
而n/k的k取法只有√n种,直接枚举即可。
//by zykykyk #include<cstdio> #include<ctime> #include<iostream> #include<algorithm> #include<cmath> #include<string> #define rg register #define il inline #define vd void #define ll long long #define For(i,x,y) for (rg int i=(x);i<=(y);i++) #define Dow(i,x,y) for (rg int i=(x);i>=(y);i--) #define cross(i,k) for (rg int i=first[k];i;i=last[i]) using namespace std; il ll max(ll x,ll y){return x>y?x:y;} il ll min(ll x,ll y){return x<y?x:y;} il ll read(){ ll x=0;int ch=getchar(),f=1; while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar(); if (ch=='-'){f=-1;ch=getchar();} while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} return x*f; } int T,a,b,c,d,A,B,C,D,ans; int main(){ T=read(); while (T--){ a=read(),b=read(),c=read(),d=read(),ans=0; int l=max(max(sqrt(a-1),sqrt(b)),max(sqrt(c-1),sqrt(d))); For(i,1,l){ int B=b/i,A=(a-1)/i,D=d/i,C=(c-1)/i; if (B>A&&D>C) ans=max(ans,i); if (A) if (b/A>(a-1)/A&&d/A>(c-1)/A) ans=max(ans,A); if (B) if (b/B>(a-1)/B&&d/B>(c-1)/B) ans=max(ans,B); if (C) if (b/C>(a-1)/C&&d/C>(c-1)/C) ans=max(ans,C); if (D) if (b/D>(a-1)/D&&d/D>(c-1)/D) ans=max(ans,D); } printf("%d\n",ans); } }