题目描述

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

提示

Explanation: Byteasar will produce four solar panels of the following sizes: 8*8 (a single cell), 7*14 (two cells), 4*22 or 6*22 (22 or 33 cells respectively), and 5*20 (four cells).

枚举区间内每个数求gcd显然不可做,我们不妨换一种思路,枚举gcd。

那么如何判断区间内是否有这个gcd?

只要(l-1)/gcd<r/gcd就能确定区间内有这个gcd了。

剩下的就是枚举gcd了,因为x/gcd只有√x种取值,所以直接整除分块,每次取块内最后一个就好了。

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int n;
int A,B,C,D;
int main()
{
scanf("%d",&n);
while(n--)
{
int ans=0;
scanf("%d%d%d%d",&A,&B,&C,&D);
A--;
C--;
int r;
for(int l=1;l<=B&&l<=D;l=r+1)
{
r=min(B/(B/l),D/(D/l));
if(A/r<B/r&&C/r<D/r)
{
ans=r;
}
}
printf("%d\n",ans);
}
}

最新文章

  1. Java的国际化(i18n)
  2. Rafy 中的 Linq 查询支持(根据聚合子条件查询聚合父)
  3. 浏览器对象模型BOM小结
  4. 1.Maven的安装以及本地仓库的配置
  5. FLUSH TABLES WITH READ LOCK
  6. selenium ide 录制回放link链接报错
  7. html5 搖一搖
  8. JSON使用——获取网页返回结果是Json的代码
  9. C++设计模式之建造模式
  10. Python 修炼1
  11. Axure学习笔记(一)
  12. 【BZOJ1146】网络管理(主席树,树状数组)
  13. LuaHotUpdate原理
  14. android ui更新
  15. java_25.1字节转为字符OutputStreamWriter
  16. exgcd求解同余方程的最小正整数解 poj1061 poj2115
  17. Python使用正则表达式分割字符串
  18. 团队作业——Alpha冲刺 12/12
  19. C++中数组名和指针的区别联系
  20. *2.2.3 加入objection机制

热门文章

  1. android ActionBarSherlock使用说明
  2. Android PageAdapter翻译
  3. 关于TCP和MQTT之间的转换
  4. FileShare枚举的使用(文件读写锁) - (转载)
  5. 从github checkout子文件夹
  6. Luogu P1306 斐波那契公约数
  7. [Oracle]如何取Control File 的Dump
  8. Part 6:静态文件--Django从入门到精通系列教程
  9. 写入mssql出现乱码
  10. Docker容器学习梳理 - 私有仓库Registry使用