bzoj 3834 [Poi2014]Solar Panels 数论分块

时间:2022-12-28 21:20:36

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

bzoj 3834 [Poi2014]Solar Panels 数论分块

发现可以数论分块

 #include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<iostream> #define N 1007 #define Wb putchar(' ')
#define We putchar('\n')
#define rg register int
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
inline void write(int x)
{
if(x<) putchar('-'),x=-x;
if (x==) putchar();
int num=;char c[];
while(x) c[++num]=(x%)+,x/=;
while(num) putchar(c[num--]);
} int ans;
int mx1,mn1,mx2,mn2; int main()
{
int T=read();
while(T--)
{
mn1=read(),mx1=read();
mn2=read(),mx2=read();
if (mx1>mx2) swap(mx1,mx2),swap(mn1,mn2);
ans=;
if (mx1>=mn2) ans=mx1;
else
{
mn1--,mn2--;
for (rg i=mx1,last;i>=;i=last)
{
last=max(mx1/(mx1/i+),mx2/(mx2/i+));
if (mn1>=i) last=max(last,mn1/(mn1/i+));
if (mn2>=i) last=max(last,mn2/(mn2/i+));
if (mx1/i-mn1/i>&&mx2/i-mn2/i>)
{
ans=i;
break;
}
}
}
write(ans),We;
}
}

bzoj 3834 [Poi2014]Solar Panels 数论分块的更多相关文章

  1. 【bzoj3834】&lbrack;Poi2014&rsqb;Solar Panels 数论

    题目描述 Having decided to invest in renewable energy, Byteasar started a solar panels factory. It appea ...

  2. 【BZOJ】3834&colon; &lbrack;Poi2014&rsqb;Solar Panels

    http://www.lydsy.com/JudgeOnline/problem.php?id=3834 题意:求$max\{(i,j)\}, smin<=i<=smax, wmin&lt ...

  3. 【BZOJ3834】&lbrack;Poi2014&rsqb;Solar Panels 分块好题

    [BZOJ3834][Poi2014]Solar Panels Description Having decided to invest in renewable energy, Byteasar s ...

  4. BZOJ3834&lbrack;Poi2014&rsqb;Solar Panels——分块

    题目描述 Having decided to invest in renewable energy, Byteasar started a solar panels factory. It appea ...

  5. 「BZOJ 2440」完全平方数「数论分块」

    题意 \(T\)组数据,每次询问第\(k\)个无平方因子的数(\(1\)不算平方因子),\(T\leq 50,k\leq 10^9\) 题解 \(k\)的范围很大,枚举肯定不行,也没什么奇妙性质,于是 ...

  6. &lbrack;POI2014&rsqb;Solar Panels

    题目大意: $T(T\le1000)$组询问,每次给出$A,B,C,D(A,B,C,D\le10^9)$,求满足$A\le x\le B,C\le y\le D$的最大的$\gcd(x,y)$. 思路 ...

  7. BZOJ3834 &lbrack;Poi2014&rsqb;Solar Panels 【数论】

    题目链接 BZOJ3834 题解 容易想到对于\(gcd(x,y) = D\),\(d\)的倍数一定存在于两个区间中 换言之 \[\lfloor \frac{a - 1}{D} \rfloor &lt ...

  8. BZOJ3834&colon;Solar Panels (分块)

    题意 询问两个区间[smin,smax],[wmin,smax]中是否存在k的倍数,使得k最大 分析 将其转化成\([\frac{smin-1}k,\frac{smax}k],[\frac{wmin- ...

  9. BZOJ3834 &colon; &lbrack;Poi2014&rsqb;Solar Panels

    问题相当于找到一个最大的k满足在$[x_1,x_2]$,$[y_1,y_2]$中都有k的倍数 等价于$\frac{x_2}{k}>\frac{x_1-1}{k}$且$\frac{y_2}{k}& ...

随机推荐

  1. http&colon;&sol;&sol;www&period;microsoft&period;com&sol;en-pk&sol;download&sol;details&period;aspx&quest;id&equals;40762

    http://www.microsoft.com/en-pk/download/details.aspx?id=40762

  2. Android 使用 ksoap2-android 访问WebService&lpar;C&num;&rpar;

    Android 客户端与后台数据交互的方式有很多种.今天这里记录一下,与WebService的数据交互. 新建一个简单的WebService 创建方式如下: 创建好的项目是这样的. 我们在里面写几句简 ...

  3. 触摸与手势学习-swift

    触摸是一个UITouch对象,该对象放在一个UIEvent中,每个UIEvent包含一个或多个UITouch对象,每个UITouch对象对应一个手指.系统将UIEvent发送到应用程序上,最后应用程序 ...

  4. css案例学习之用thead、tbody、tfoot实现漂亮的table布局

    首先说说thead.tbody.tfoot <thead> <tbody> <tfoot> 无论前后顺序如何改变, <thead> 内的元素总是在表的最 ...

  5. javascript 中字符串之比较

    <script type="text/javascript"> var string1="apple"; var string2="Ban ...

  6. mockito中两种部分mock的实现,spy、callRealMethod

    什么是类的部分mock(partial mock)?A:部分mock是说一个类的方法有些是实际调用,有些是使用mockito的stubbing(桩实现). 为什么需要部分mock? A:当需要测试一个 ...

  7. sql server 的datediff函数

    这两天要把一个sqlserver数据库的程序改成oracle的,发现两个数据库之间的函数很多不一样.sqlserver的数据库中的DateDiff 函数用法解释如下: 描述 返回两个日期之间的时间间隔 ...

  8. 算法分析&vert; 小o和小ω符号

    渐近分析的主要思想是对不依赖于机器特定常数的算法的效率进行测量,主要是因为该分析不需要实现算法并且要比较程序所花费的时间. 我们已经讨论了三个主要的渐近符号.本文我们使用以下2个渐近符号表示算法的时间 ...

  9. c&num;中常用集合类和集合接口之集合类系列【转】

    常用集合接口系列:http://www.cnblogs.com/fengxiaojiu/p/7997704.html 常用集合类系列:http://www.cnblogs.com/fengxiaoji ...

  10. Android万能的指示器

    说到 ViewPager 指示器,想必大家都不陌生,绝大部分应用中都有这个.使用频率非常之高.但系统对它的支持并不好,自带的 PagerTabStrip 和 PagerTitleStrip 太弱,很难 ...