P1009 阶乘之和求助
  • 板块学术版
  • 楼主super_zzr
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/4/5 13:55
  • 上次更新2023/10/23 19:22:20
查看原帖
P1009 阶乘之和求助
966353
super_zzr楼主2023/4/5 13:55
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#define endl "\n"
using namespace std;
int a[10000005],c[10000005],a1=1,c1=1;
void jia(){
	int l=max(a1,c1);
	for(int i=1;i<=l;i++){
		c[i]+=a[i];
		c[i+1]=c[i]/10;
		c[i]%=10;
	}
	if(c[l+1]){
	    c1+=1;
	}
}
void cheng(int k){
	int tot=-1,b[3];
	while(k!=0){
		b[++tot]=k%10;
		k/=10; 
	}
	for(int i=1;i<=a1;i++){
		for(int j=0;j<=tot;j++){
			a[i+j+1]+=(a[i+j]*b[j])/10;
			a[i+j]=(a[i+j]*b[j])%10;
		}
	}
	a[a1+tot+1]+=a[a1+tot]/10;
	a[a1+tot]=a[a1+tot]%10;
	if(a[a1+tot+1+1]){
		a1=a1+tot+1+1;
	}
	else if(a[a1+tot+1]){
		a1=a1+tot+1;
	}
	else{
		a1=a1+tot;
	}
}
int main(){
	long long n,cnt=0;
	cin>>n;
	a[1]=1;
	c[1]=1;
	for(long long i=2;i<=n;i++){
		cheng(i);
		jia();
	}
	for(int i=c1;i>=1;i--){
		cout<<c[i];
	}
	return 0;
}
2023/4/5 13:55
加载中...