码迷,mamicode.com
首页 > 其他好文 > 详细

Luogu3579 Solar Panels

时间:2018-10-31 13:56:00      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:math   getch   string   取整   str   \n   print   ==   min   


整除分块枚举。。。

真的没有想到会这么简单。

要使一个数 \(p\) 满足 条件, 则 存在\(x, y\)\(a<=x \times p<=b\ \&\&\ c<=y \times p <=d\)

\(p\) 除掉 则

   \(\left\lceil\dfrac{a}{p}\right\rceil <=y <=\left\lfloor\dfrac{b}{p}\right\rfloor\)

   \(\left\lceil\dfrac{c}{p}\right\rceil <=y <=\left\lfloor\dfrac{d}{p}\right\rfloor\)

把向上取整变为向下取整

   \(\left\lfloor\dfrac{a+p-1}{p}\right\rfloor <= \left\lfloor\dfrac{b}{p}\right\rfloor\)

   \(\left\lfloor\dfrac{b+p-1}{p}\right\rfloor <= \left\lfloor\dfrac{d}{p}\right\rfloor\)

然后就变成了 :

  \(\left\lfloor\dfrac{a-1}{p}\right\rfloor < \left\lfloor\dfrac{b}{p}\right\rfloor\)

  \(\left\lfloor\dfrac{b-1}{p}\right\rfloor < \left\lfloor\dfrac{d}{p}\right\rfloor\)

最后整除分块。 只需按照 \(b/p\)\(d/p\) 相同时进行分类。 这样能使 \(b/p\)\(d/p\)相等的同时 \(c/p\)\(d/p\)尽量小, 更可能满足条件

#include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
#define R register
using namespace std;

inline int read() {
    int X = 0, p = 1; char c = getchar();
    for (; c > '9' || c < '0'; c =  getchar())
        if (c == '-') p = -1;
    for (; c >= '0' && c <= '9'; c = getchar())
        X = X * 10 + c - '0';
    return X * p;
}

inline void cmax(int &A, int B) {
    if (A < B) A = B;
}

inline int cmin(int A, int B) {
    return A > B ? B : A;
}

void work() {
    int ans = 1;
    int a = rd - 1, b = rd, c = rd - 1, d = rd;
    for (R int i = 1, j = 1, up = cmin(b, d); i <= up; i = j + 1) {
        j = cmin(b / (b / i), d / (d / i));
        if (b / j > a / j && d / j > c / j) cmax(ans, j);
    }
    printf("%d\n", ans);
}

int main()
{
    int n = rd;
    for (; n; --n) work(); 
}

Luogu3579 Solar Panels

标签:math   getch   string   取整   str   \n   print   ==   min   

原文地址:https://www.cnblogs.com/cychester/p/9880861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!