bzoj3834 [Poi2014]Solar Panels 分块

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

Solution


考虑枚举答案,答案合法当且仅当 a 1 a n s < b a n s c 1 a n s < d a n s
这里原本想的是 a a n s + 西 b a n s 这样子的,结果有特殊情况会不好处理
看到整除下取整就上分块啊

洛谷上有一个点过不了是怎么回事,希望能有人教QAQ

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define drp(i,st,ed) for (int i=st;i>=ed;--i)
#define min(x,y) ((x)<(y)?(x):(y))

int read() {
    int x=0,v=1; char ch=getchar();
    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
    return x*v;
}

inline void writeln(int x){
    char ch[21]={}; int i=0;
    if (x<0) putchar('-');
    do {ch[++i]='0'+x%10;} while (x/=10);
    for (;i;) putchar(ch[i--]);
    putchar('\n');
}

int main(void) {
    for (int T=read();T--;) {
        int a=read()-1,b=read(),c=read()-1,d=read();
        if (b>d) std:: swap(a,c),std:: swap(b,d);
        int ans=0,l1,l2,r1,r2;
        for (int i=1,last;i<=b;i=last+1) {
            last=min(b/(b/i),d/(d/i));
            if (i<=a) last=min(last,a/(a/i));
            if (i<=c) last=min(last,c/(c/i));
            l1=a/i,r1=b/i;
            l2=c/i,r2=d/i;
            if (l1>=r1||l2>=r2) continue;
            ans=last;
        }
        writeln(ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jpwang8/article/details/80560816
今日推荐