PKU2018校赛 H题 Safe Upper Bound

http://poj.openjudge.cn/practice/C18H

题目

算平均数用到公式[ar{x}=frac{x_1+x_2+x_3+cdots+x_n}{n}]

但如果用int型计算,那么(x_1+x_2+x_3+cdots+x_n)可能会超过(2^{31}-1)

算6个数的平均数可以这么算

Calculate the average of(x_1,x_2,x_3)
[ar{x}_1=frac{x_1+x_2+x_3}{3}]
Calculate the average of(x_4,x_5,x_6)
[ar{x}_2=frac{x_4+x_5+x_6}{3}]
Calculate the average of(ar{x}_1,ar{x}_2)
[ar{x}=frac{ar{x}_1+ar{x}_2}{2}]
In this way, as you can see, we actually add up at most $3$ integers at one time, instead of adding all the $6$ integers together. Therefore, as long as all the integers are not greater than (leftlfloor {left( {{2^{31}} - 1} ight)/3} ight floor ), we are not at risk of getting an overflow result. Thus, we call the value $71582782$ the Safe Upper Bound of $6$.

输入N,输出N的安全上界

题解

某日无聊翻openjudge的poj队伍,发现了PKU的校赛,想找一道最简单的题满足虚荣心:(

看了好久没看懂在干什么,看样例用计算器猜是$2^{31}-1$除以N的最大素因子

数论不行:(

照着书抄了个Pollard Rho+Miller-Rabin算法 TLE(其实根本就不知道复杂度

于是尝试Eratosthenes线性筛……可是需要开的数组太大……貌似无解了

其实还是自己太菜:(

为什么可以这么做呢……猜可能和这个过程有关
[leftlfloor {frac{{a + b}}{2}} ight floor  = frac{{a + b}}{2} - frac{{left( {a + b} ight)\% 2}}{2}]
[leftlfloor {frac{{c + d}}{2}} ight floor  = frac{{c + d}}{2} - frac{{left( {c + d} ight)\% 2}}{2}]
[leftlfloor {frac{{a + b}}{2}} ight floor  + leftlfloor {frac{{c + d}}{2}} ight floor  = frac{{a + b + c + d}}{2} - frac{{a\% 2 + b\% 2 + c\% 2 + d\% 2}}{2}]
[leftlfloor {frac{{leftlfloor {frac{{a + b}}{2}} ight floor  + leftlfloor {frac{{c + d}}{2}} ight floor }}{2}} ight floor  = frac{{a + b + c + d}}{4} - frac{{a\% 2 + b\% 2 + c\% 2 + d\% 2}}{4}]
至于(frac{{a\% 2 + b\% 2 + c\% 2 + d\% 2}}{4})是否等于({left( {a + b + c + d} ight)\% 4})

我还是菜鸟,等以后变强了再看看……推广也只有以后了

空间问题抄了UESTC大神的代码

https://vjudge.net/solution/15934751

看了以后感觉自己真的太菜了:(

这差距不是一点啊……还得加油

原文地址:https://www.cnblogs.com/sahdsg/p/10369063.html