BZOJ3834: [Poi2014]Solar Panels
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3834
题意:求max(gcd(i,j)) 其中 a<=i<=b,c<=j<=d
题解:
先考虑怎么判断是否存在两个给定范围内的数使得gcd(i,j)>=k
这很好办,我们只要判断 b/k>(a-1)/k &&d/k>(c-1)/k
那我们枚举是个好方法,但是超时,我们发现上面的四个值每个都只有根号级别的数量,那我们分块即可。
注意用块内的最后一个数来更新答案。
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 3 #include<cstdlib> 4 5 #include<cmath> 6 7 #include<cstring> 8 9 #include<algorithm> 10 11 #include<iostream> 12 13 #include<vector> 14 15 #include<map> 16 17 #include<set> 18 19 #include<queue> 20 21 #include<string> 22 23 #define inf 1000000000 24 25 #define maxn 200000+5 26 27 #define maxm 200000+5 28 29 #define eps 1e-10 30 31 #define ll long long 32 33 #define pa pair<int,int> 34 35 #define for0(i,n) for(int i=0;i<=(n);i++) 36 37 #define for1(i,n) for(int i=1;i<=(n);i++) 38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 42 43 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 44 45 #define for5(n,m) for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) 46 47 #define mod 1000000007 48 49 using namespace std; 50 51 inline int read() 52 53 { 54 55 int x=0,f=1;char ch=getchar(); 56 57 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 58 59 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 60 61 return x*f; 62 63 } 64 65 int main() 66 67 { 68 69 freopen("input.txt","r",stdin); 70 71 freopen("output.txt","w",stdout); 72 73 int T=read(); 74 while(T--) 75 { 76 int a=read(),b=read(),c=read(),d=read(),ans; 77 if(b>d)swap(a,c),swap(b,d); 78 if(b>=c){printf("%d\n",b);continue;} 79 a--;c--; 80 for(int i=1,j;i<=b;i=j+1) 81 { 82 j=min(b/(b/i),d/(d/i)); 83 if(i<=a)j=min(j,a/(a/i)); 84 if(i<=c)j=min(j,c/(c/i)); 85 if(b/j>a/j&&d/j>c/j)ans=j; 86 } 87 printf("%d\n",ans); 88 } 89 90 return 0; 91 92 }