bzoj3834 [Poi2014]Solar Panels 分块

版权声明:虽然是个蒟蒻但是转载还是要说一声的哟 https://blog.csdn.net/jpwang8/article/details/80560816

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


考虑枚举答案,答案合法当且仅当a1ans<bansc1ans<dans
这里原本想的是aans+西bans这样子的,结果有特殊情况会不好处理
看到整除下取整就上分块啊

洛谷上有一个点过不了是怎么回事,希望能有人教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;
}
阅读更多
换一批

没有更多推荐了,返回首页