[POI2014]Solar Panels

题目大意:
  $T(Tle1000)$组询问,每次给出$A,B,C,D(A,B,C,Dle10^9)$,求满足$Ale xle B,Cle yle D$的最大的$gcd(x,y)$。

思路:
  令$n=gcd(x,y)$,则若$n$为合法的答案,当且仅当$lfloorfrac{A-1}n floor<lfloorfrac Bn floor,lfloorfrac{C-1}n floor<lfloorfrac Dn floor$。
  考虑数论分块,每次用块内最大值更新答案即可。

 1 #include<cstdio>
 2 #include<cctype>
 3 #include<algorithm>
 4 inline int getint() {
 5     register char ch;
 6     while(!isdigit(ch=getchar()));
 7     register int x=ch^'0';
 8     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
 9     return x;
10 }
11 int main() {
12     for(register int T=getint();T;T--) {
13         const int a=getint()-1,b=getint(),c=getint()-1,d=getint();
14         int ans=0;
15         for(register int i=1,j;i<=std::min(b,d);i=j+1) {
16             j=std::min(b/(b/i),d/(d/i));
17             if(a/j<b/j&&c/j<d/j) ans=std::max(ans,j);
18         }
19         printf("%d
",ans);
20     }
21     return 0;
22 }
原文地址:https://www.cnblogs.com/skylee03/p/8643924.html