【bzoj3834】[Poi2014]Solar Panels 数论

题目描述

Having decided to invest in renewable energy, Byteasar started a solar panels factory. It appears that he has hit the gold as within a few days  clients walked through his door. Each client has ordered a single rectangular panel with specified width and height ranges.
The panels consist of square photovoltaic cells. The cells are available in all integer sizes, i.e., with the side length integer, but all cells in one panel have to be of the same size. The production process exhibits economies of scale in that the larger the cells that form it, the more efficient the panel. Thus, for each of the ordered panels, Byteasar would like to know the maximum side length of the cells it can be made of.
n组询问,每次问smin<=x<=smax, wmin<=y<=wmax时gcd(x, y)的最大值。

输入

The first line of the standard input contains a single integer N(1<=N<=1000): the number of panels that were ordered. The following   lines describe each of those panels: the i-th line contains four integers Smin,Smax,Wmin,Wmax(1<=Smin<=Smax<=10^9,1<=Wmin<=Wmax<=10^9), separated by single spaces; these specify the minimum width, the maximum width, the minimum height, and the maximum height of the i-th panel respectively.

输出

Your program should print exactly n lines to the standard output. The i-th line is to give the maximum side length of the cells that the i-th panel can be made of.

样例输入

4
3 9 8 8
1 10 11 15
4 7 22 23
2 5 19 24

样例输出

8
7
2
5


题解

数论

结论:区间$(l,r]$中出现$n$的倍数的充要条件是$lfloorfrac rn floor>lfloorfrac ln floor$。

于是可以枚举$i$,看是否在两段区间内都出现过。可以通过枚举商将时间复杂度将至$O(nsqrt a)$。

注意在枚举商的时候要使用最后一个商与$b/i$和$d/i$相等的$last$值计算。

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
	int T , a , b , c , d , i , last , ans;
	scanf("%d" , &T);
	while(T -- )
	{
		scanf("%d%d%d%d" , &a , &b , &c , &d);
		for(i = 1 ; 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
" , ans);
	}
	return 0;
}

 

原文地址:https://www.cnblogs.com/GXZlegend/p/7434565.html