http://poj.openjudge.cn/practice/C18H
题目
算平均数用到公式\[\bar{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\)
\[\bar{x}_1=\frac{x_1+x_2+x_3}{3}\]
Calculate the average of\(x_4,x_5,x_6\)
\[\bar{x}_2=\frac{x_4+x_5+x_6}{3}\]
Calculate the average of\(\bar{x}_1,\bar{x}_2\)
\[\bar{x}=\frac{\bar{x}_1+\bar{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 \(\left\lfloor {\left( {{2^{31}} - 1} \right)/3} \right\rfloor \), 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线性筛……可是需要开的数组太大……貌似无解了
其实还是自己太菜:(
为什么可以这么做呢……猜可能和这个过程有关
\[\left\lfloor {\frac{{a + b}}{2}} \right\rfloor = \frac{{a + b}}{2} - \frac{{\left( {a + b} \right)\% 2}}{2}\]
\[\left\lfloor {\frac{{c + d}}{2}} \right\rfloor = \frac{{c + d}}{2} - \frac{{\left( {c + d} \right)\% 2}}{2}\]
\[\left\lfloor {\frac{{a + b}}{2}} \right\rfloor + \left\lfloor {\frac{{c + d}}{2}} \right\rfloor = \frac{{a + b + c + d}}{2} - \frac{{a\% 2 + b\% 2 + c\% 2 + d\% 2}}{2}\]
\[\left\lfloor {\frac{{\left\lfloor {\frac{{a + b}}{2}} \right\rfloor + \left\lfloor {\frac{{c + d}}{2}} \right\rfloor }}{2}} \right\rfloor = \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} \right)\% 4}\)
我还是菜鸟,等以后变强了再看看……推广也只有以后了
空间问题抄了UESTC大神的代码
https://vjudge.net/solution/15934751
看了以后感觉自己真的太菜了:(
这差距不是一点啊……还得加油
原文地址:https://www.cnblogs.com/sahdsg/p/10369063.html