HDOJ 1018(数论)
题意描述
Problem Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.
Input Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107 on each line.
Output The output contains the number of digits in the factorial of the integers appearing in the input.
Sample Input 2 10 20
Sample Output 7 19
思路
刚看到题的一瞬间,果断写了个高精度交了上去,TLE了,再看数据范围,嚯!1e7,然后就去翻了题解,发现是数论问题,求阶乘位数有两种方法:
1.10m<n!<10(m+1) 若求得M,则M+1为答案。对方程两边以10为底求对数,得M<log10(n!)<M+1,通过循环求值即可 2.斯特林公式:n! ≈ sqrt(2* n * pi)* (n/e)^n,则 M+1=(int)(0.5 * log(2.0 * n *PI)+n * log(n)-n)/(log(10.0)) )+1;
AC代码
方法1 复杂度O(n):
#include<vector>
#include<iostream>
#include<cstring>
#include<queue>
#include<set>
#include<cmath>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0);
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int N=15;
const int INF=0x3f3f3f3f;
int main() {
IOS;
int T;
cin>>T;
while(T--){
int n;
cin>>n;
double ans=0;
for(int i=1;i<=n;i++){
ans+=log(i)/log(10);
}
cout<<(int)ans+1<<endl;
}
return 0;
}
方法2 复杂度O(1):
#include<vector>
#include<iostream>
#include<cstring>
#include<queue>
#include<set>
#include<cmath>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0);
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int N=15;
const double PI=3.1415926;
const int INF=0x3f3f3f3f;
int main() {
IOS;
int T;
cin>>T;
while(T--){
int n;
cin>>n;
double ans=0;
cout<<(int)((0.5*log(2.0*n*PI)+n*log(n)-n)/(log(10.0)))+1<<endl;
}
return 0;
}
- 通过shell脚本得到数据字典的信息 (r2笔记72天)
- 机器学习算法实践——K-Means算法与图像分割
- 利用 Python、SciKit 和文本分类来构建客户行为描述模型
- 使用Python爬取社交网络数据分析
- PHP爬虫源码:百万级别知乎用户数据爬取与分析
- 使用Python抓取欧洲足球联赛数据
- python爬取百度新闻:分析共享单车火爆背后有哪些规则?
- Python爬虫(urllib2+bs4)数据采集:分析找出百度贴吧谁是水贴王
- 学界 | OpenAI 发布稀疏计算内核,更宽更深的网络,一样的计算开销
- 【手把手教你做项目】自然语言处理:单词抽取/统计
- Kaggle赛题解析:逻辑回归预测模型实现
- R语言 使用BP神经网络进行银行客户信用评估
- 使用R语言挖掘QQ群聊天记录
- 解析滴滴算法大赛---GBDT进行数据预测
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法