Sicily 1119. Factstone Benchmark | 使用log函数缩小数值范围
来源:互联网 发布:linux链接网络 编辑:程序博客网 时间:2024/05/29 13:20
题目:
• 题意:
1960年发行了4位计算机,从此以后每过10年,计算机的位数变成两倍。输入某一个年份,求出在这个年份的最大的整数n使得n!能被一个字表示。
• 限制:年份1960<=n<=2160,且n%10 == 0
• 解法:
由于位长最多为2^22,能够表示的数范围很大,所以我们考虑使用log来缩小数值范围
如果n!能够被位长为 bit_len 的字表示,那么应该
有 n ! < 2^bitLen
也就是,log2(n!) < bitLen
即是,log2(1)+log2(2)+….+log2(n-1)+log2(n) < bitLen
那么,其实我们只需要从小到大枚举n,再做判断就可以了
代码:
// Problem#: 1119// Submission#: 5152249// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <iostream> #include <algorithm>#include <vector>#include <cmath>using namespace std;int getMaxN(int year){ int bitLen = 1 << ((year - 1960) / 10 + 2), n = 1; double tmp = 0; while (tmp / log(2) < bitLen) { tmp += log(++n); } return n - 1;}int main() { int year; while (cin >> year&&year) { cout << getMaxN(year) << endl; } //system("pause");}
阅读全文
0 0
- Sicily 1119. Factstone Benchmark | 使用log函数缩小数值范围
- Sicily 1119. Factstone Benchmark
- Sicily 1119. Factstone Benchmark
- Sicily 1119. Factstone Benchmark
- Sicily 1119. Factstone Benchmark
- sicily 1119. Factstone Benchmark
- Sicily.1119. Factstone Benchmark(代数转换成两边取log)
- Sicily 1119 Factstone Benchmark
- 1119. Factstone Benchmark
- 1119. Factstone Benchmark
- 1119. Factstone Benchmark
- 1119. Factstone Benchmark
- POJ 2661-Factstone Benchmark(log()的应用)
- Factstone Benchmark
- Factstone Benchmark
- Factstone Benchmark
- Factstone Benchmark
- Factstone Benchmark
- # Exceptionin thread "main" java.lang.UnsatisfiedLinkError:org.apache.hadoop.util.NativeCrc32.native
- 软件
- 关于redis还有zookeeper
- C语言基础
- javascript的本地对象、内置对象、宿主对象
- Sicily 1119. Factstone Benchmark | 使用log函数缩小数值范围
- 九度OJ 1100:最短路
- JDK安装与配置
- Hadoop实践(四)---生成集群上的监视和调试
- C#使用 DeflateStream类 解压zlib数据流
- 如何设计数据库表?
- Android IntentService 使用教程
- 肖特基、整流、开关、快恢复二极管的区别
- maven详解3