当前位置:网站首页>Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
2022-07-07 01:02:00 【Jun Yi_ noip】
【 Topic link 】
ybt 1172: seek 10000 within n The factorial
OpenJudge NOI 1.6 14: seek 10000 within n The factorial
【 Topic test site 】
1. High precision
Investigate : High precision multiplication and low precision
Explanation of high-precision calculation
【 Their thinking 】
First find the largest digit of the result , That is to say 10000 ! 10000! 10000! Number of digits .
A positive integer is known x The number of digits of is n, So there are : n = ⌊ l g x ⌋ + 1 n = \lfloor lgx \rfloor + 1 n=⌊lgx⌋+1( l g x lgx lgx: With 10 Bottom x The logarithmic )
Bits are ⌊ l g 10000 ! ⌋ + 1 ≤ ⌊ l g 1000 0 10000 ⌋ + 1 = ⌊ 10000 ⋅ l g 10000 ⌋ + 1 = ⌊ 10000 ∗ 4 ⌋ + 1 = 40001 \lfloor lg10000! \rfloor + 1\le \lfloor lg10000^{10000} \rfloor + 1=\lfloor 10000\cdot lg10000 \rfloor + 1=\lfloor 10000*4 \rfloor + 1 = 40001 ⌊lg10000!⌋+1≤⌊lg1000010000⌋+1=⌊10000⋅lg10000⌋+1=⌊10000∗4⌋+1=40001
The length of the number array is 40005 that will do .
Because of the demand for 10000 The factorial , The number multiplied each time is less than or equal to 10000 The number of , Is a low precision number . The result of factorial is a high-precision number . So we should use high precision times low precision . If you use high precision times high precision , The complexity of the algorithm will increase , It may time out .
【 Solution code 】
solution 1: Use functions and arrays
#include <bits/stdc++.h>
using namespace std;
#define N 40005
void setLen(int a[], int i)
{
while(a[i] == 0 && i > 1)// Remove the superfluous 0
i--;
a[0] = i;
}
void Multiply(int a[], int b)//a *= b High precision multiplication and low precision
{
int c = 0, i;
for(i = 1; i <= a[0]; ++i)
{
a[i] = a[i]*b + c;
c = a[i] / 10;
a[i] %= 10;
}
while(c > 0)
{
a[i] = c % 10;
c /= 10;
i++;
}
setLen(a, i);
}
void toNum(char s[], int a[])
{
a[0] = strlen(s);
for(int i = 1; i <= a[0]; ++i)
a[i] = s[a[0] - i] - '0';
}
void showNum(int a[])
{
for(int i = a[0];i >= 1;--i)
cout << a[i];
}
int main()
{
int a[N] = {
1, 1}, n;// High precision number a The initial value is 1
cin >> n;
for(int i = 1; i <= n; ++i)
Multiply(a, i);
showNum(a);
return 0;
}
solution 2: Overloaded operator in class
#include <bits/stdc++.h>
using namespace std;
#define N 40005
struct HPN
{
int a[N];// Array of numbers
HPN()
{
memset(a, 0, sizeof(a));
}
HPN(char s[])
{
memset(a, 0, sizeof(a));
int len = strlen(s);
for(int i = 0; i < len; ++i)
a[len - i] = s[i] - '0';
a[0] = len;
}
void setLen(int i)
{
while(a[i] == 0 && i > 1)// Remove the superfluous 0
i--;
a[0] = i;
}
void operator *= (int b)//a *= b High precision multiplication and low precision
{
int c = 0, i;
for(i = 1; i <= a[0]; ++i)
{
a[i] = a[i]*b + c;
c = a[i] / 10;
a[i] %= 10;
}
while(c > 0)
{
a[i] = c % 10;
c /= 10;
i++;
}
setLen(i);
}
void show()
{
for(int i = a[0]; i >= 1; --i)
cout << a[i];
}
};
int main()
{
HPN a("1");// High precision digital a The initial value is 1
int n;
cin >> n;
for(int i = 1; i <= n; ++i)
a *= i;// High precision multiplication and low precision
a.show();
return 0;
}
边栏推荐
- Link sharing of STM32 development materials
- Build your own website (17)
- Installation and testing of pyflink
- 第七篇,STM32串口通信编程
- Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
- Windows installation mysql8 (5 minutes)
- paddlehub应用出现paddle包报错的问题
- [batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
- 【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
- from . cv2 import * ImportError: libGL. so. 1: cannot open shared object file: No such file or direc
猜你喜欢

【批处理DOS-CMD命令-汇总和小结】-字符串搜索、查找、筛选命令(find、findstr),Find和findstr的区别和辨析

ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
![[software reverse automation] complete collection of reverse tools](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[software reverse automation] complete collection of reverse tools

随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP

《安富莱嵌入式周报》第272期:2022.06.27--2022.07.03

Dr selection of OSPF configuration for Huawei devices
![Explain in detail the matrix normalization function normalize() of OpenCV [norm or value range of the scoped matrix (normalization)], and attach norm_ Example code in the case of minmax](/img/87/3fee9e6f687b0c3efe7208a25f07f1.png)
Explain in detail the matrix normalization function normalize() of OpenCV [norm or value range of the scoped matrix (normalization)], and attach norm_ Example code in the case of minmax

Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23

pyflink的安装和测试
Summary of being a microservice R & D Engineer in the past year
随机推荐
Leetcode(547)——省份数量
Learning notes 5: ram and ROM
深度学习之环境配置 jupyter notebook
Configuring the stub area of OSPF for Huawei devices
Part VI, STM32 pulse width modulation (PWM) programming
Build your own website (17)
Deep learning framework TF installation
OSPF configuration command of Huawei equipment
省市区三级坐标边界数据csv转JSON
【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
Dell Notebook Periodic Flash Screen Fault
[牛客] [NOIP2015]跳石头
浅谈测试开发怎么入门,如何提升?
【批处理DOS-CMD命令-汇总和小结】-字符串搜索、查找、筛选命令(find、findstr),Find和findstr的区别和辨析
Telerik UI 2022 R2 SP1 Retail-Not Crack
Provincial and urban level three coordinate boundary data CSV to JSON
深度学习之线性代数
Deep understanding of distributed cache design
[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error
Advanced learning of MySQL -- basics -- multi table query -- joint query