bzoj 3834 [Poi2014]Solar Panels 数论分块

3834: [Poi2014]Solar Panels

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 367  Solved: 285
[Submit][Status][Discuss]

Description

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)的最大值。

 

Input

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.

 

Output

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.

 

Sample Input

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

Sample Output

8
7
2
5

HINT

 

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).

 

 

 

Source

鸣谢zhonghaoxi

 

 

发现可以数论分块 

 1 #include<cstring>
 2 #include<cmath>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<iostream>
 6 
 7 #define N 1007
 8 
 9 #define Wb putchar(' ')
10 #define We putchar('\n')
11 #define rg register int
12 using namespace std;
13 inline int read()
14 {
15     int x=0,f=1;char ch=getchar();
16     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
17     while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
18     return x*f;
19 }
20 inline void write(int x)
21 {
22     if(x<0) putchar('-'),x=-x;
23     if (x==0) putchar(48);
24     int num=0;char c[15];
25     while(x) c[++num]=(x%10)+48,x/=10;
26     while(num) putchar(c[num--]);
27 }
28 
29 int ans;
30 int mx1,mn1,mx2,mn2;
31 
32 
33 int main()
34 {
35     int T=read();
36     while(T--)
37     {
38         mn1=read(),mx1=read();
39         mn2=read(),mx2=read();
40         if (mx1>mx2) swap(mx1,mx2),swap(mn1,mn2);
41         ans=1;
42         if (mx1>=mn2) ans=mx1;
43         else
44         {
45             mn1--,mn2--;
46             for (rg i=mx1,last;i>=1;i=last)
47             {
48                 last=max(mx1/(mx1/i+1),mx2/(mx2/i+1));
49                 if (mn1>=i) last=max(last,mn1/(mn1/i+1));
50                 if (mn2>=i) last=max(last,mn2/(mn2/i+1));
51                 if (mx1/i-mn1/i>0&&mx2/i-mn2/i>0)
52                 {
53                     ans=i;
54                     break;
55                 }
56             }
57         }
58         write(ans),We;
59     }
60 }

 

转载于:https://www.cnblogs.com/fengzhiyuan/p/8983619.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值