【BZOJ】【P3834】【Poi2014】【Solar Panels】【题解】【分块统计】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3834

设gcd(x,y)=k x∈[a,b] y∈[c,d]

=>gcd(x/k,y/k)=1 x∈((a-1)/k,b/k] y∈((c-1)/k,d/k]

这四个都只有sqrtn种取值……没了

Code:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int T;scanf("%d",&T);
	while(T--){
		int a,b,c,d,ans=0;scanf("%d%d%d%d",&a,&b,&c,&d);
		if(b>d)swap(a,c),swap(b,d);a--;c--;
		if(b>c){printf("%d\n",b);continue;}
		for(int i=1,j;i<=b;i=j+1){
			j=min(b/(b/i),d/(d/i));
			if(i<=a)j=min(j,a/(a/i));
			if(i<=c)j=min(j,c/(c/i));
			if(b/j>a/j&&d/j>c/j)ans=j;
		}printf("%d\n",ans);
	}
}


你可能感兴趣的:(bzoj)