当前位置:网站首页>Sum of factorials of Luogu p1009 [noip1998 popularization group]
Sum of factorials of Luogu p1009 [noip1998 popularization group]
2022-07-28 00:02:00 【Shining Sirius】
Title Description
Calculate with high precision S = 1! + 2! + 3! + ⋯+n!(n≤50).
among ! It means factorial , Defined as n!=n×(n−1)×(n−2)×2×⋯×1. for example ,5! =5×4×3×2×1=120.
Input format
A positive integer n.
Output format
A positive integer S, It means the result of calculation .
I/o sample
Input
3
Output
9
explain / Tips
【 Data range 】
about 100% The data of ,1≤n≤50.
【 Other instructions 】
notes ,《 Basic chapter in simple terms 》 Use this question as an example , But its data range is only n≤20, Using the code in the book can't pass this question .
If you want to pass this question , Please continue to learn the knowledge of high precision in Chapter 8 .
Thinking is high precision multiplication + High precision additive , It is to cover the high-precision multiplication template, and then set the high-precision processing template ,b=c=i The factorial .
Don't talk much , Go straight up C++ Code :
#include<iostream>
#include<cstring>
using namespace std;
int n,a[90],b[90],c[90],f[90],d=0,len_a,len_b=1,len_c=1,len_ans,m=1;
string s;
int main(){
cin>>n;
b[0]=1; // initialization
for(int i=1;i<=n;i++){ // Calculation i The factorial , It has been calculated i-1 The factorial
len_a=0; //i The length of
int p=i;
while(p>0){ // hold i Put in a Array
a[len_a++]=p%10;
p/=10;
}
for(int j=0;j<len_a;j++) // Calculation a*b(i*(i-1) The factorial ), namely i The factorial , If you don't understand, check online , I don't know why?
for(int k=0;k<=len_b;k++)
c[j+k]+=a[j]*b[k];
for(int j=0;j<len_c;j++) // If you need to carry, carry
if(c[j]>9) c[j+1]+=c[j]/10,c[j]%=10;
if(c[len_c]) len_c++; // See if the highest position should be carried
len_ans=len_b,len_b=len_c,m=max(m,len_c); // hold len_b Assign a value to len_ans, modify len_b Value ,m by i The length of the factorial , See if there is a carry
for(int k=len_c-1;k>=0;k--) b[k]=c[k]; // hold c Put in b Array , That is, deposit i The factorial , Next cycle b by i-1 The factorial
len_c=len_a+len_ans;
memset(c,0,sizeof(c)); // Zero clearing c Array , Ready to calculate the next factorial
for(int j=0;j<m;j++){ // High precision additive , Set the template directly
f[j]+=b[j];
if(f[j]>9) f[j+1]+=f[j]/10,f[j]%=10; // carry , Be careful not to write f[j+1]++,f[j]-=10; Because here wa I got a point
}
}
while(!f[m]&&m>0) m--; // Remove the first leading zero
for(int i=m;i>=0;i--) cout<<f[i]; // Output in reverse order
return 0; // A successful conclusion
}Last …… Let's go with a compliment !
边栏推荐
- TCP的粘包拆包问题+解决方案
- [GWCTF 2019]枯燥的抽奖
- Latex常用总结(2):输入矩阵(输入矩阵、对角阵、方程组等)
- JS提升:JS中的数组扁平化问题
- 2022 International Conference on civil, building and Environmental Engineering (iccaee 2022)
- 2022/7/26
- New technology leads new changes in marketing of large and medium-sized enterprises, and UFIDA BiP CRM is launched!
- Spark 离线开发框架设计与实现
- QT with OpenGL (shadow mapping)
- CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021
猜你喜欢

Bank marketing predicts the success rate of a customer's purchase of financial products

Shell编程规范与变量

传奇外网架设教程带图文解说——Gom引擎

liux常用命令(查看及其开放防火墙端口号+查看及其杀死进程)

Realization of gobang man-machine combat

29.学习Highcharts 使用百分比的堆叠柱形图

2022年土木,建筑与环境工程国际会议(ICCAEE 2022)
![[NCTF2019]babyRSA1](/img/c1/52e79b6e40390374d48783725311ba.gif)
[NCTF2019]babyRSA1

BUUCTF-RSA roll

Character stream learning 14.3
随机推荐
【zer0pts CTF 2022】 Anti-Fermat
BUUCTF-RSA4
Arm32 for remote debugging
重新定义分析 - EventBridge 实时事件分析平台发布
传奇外网架设教程带图文解说——Gom引擎
UE4 official AEC blueprint case course learning notes
面试官问线程安全的List,看完再也不怕了!
测试类中的断言机制
15million per day! BYD masks won a US $1billion order in California
How to use FTP to realize automatic update of WinForm
2022/7/26
Master data management theory and Practice
Join hands with Changjiang storage, jiangbolong launches the world's smallest expansion card
[ACTF新生赛2020]crypto-aes
[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters
字符流学习14.3
org.junit.runners.model. InvalidTestClassError: Invalid test class ‘com.zhj.esdemo. MysqlTests‘: 1.
Technical certification | Tupo software and Huawei cloud create a new situation of win-win cooperation
NDK 系列(6):说一下注册 JNI 函数的方式和时机
[NPUCTF2020]EzRSA