54dashabi @ 2023-07-07 20:37:46
代码如下,本地都没问题,但是评测就是MLE,求大佬解释
#include<iostream>
#include<iomanip>
#include<string>
#include<cstring>
using namespace std;
string arr[55];
int a[10100]={};
int b[10100]={};
int c[10100][10100]={};
int res[10100]={};
int apple[10010]={};
int boy[10010]={};
int car[10010]={};
string mul(string s1,string s2);
string add(string s1,string s2);
string str;
string fact(int n);
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
arr[i]=fact(i);
}
string sum;
for(int i=1;i<=n;i++){
sum=add(sum,arr[i]);
}
cout<<sum;
}
string mul(string s1,string s2){
if(s1=="0"||s2=="0"){
return "0";
}
str="";
int l1=s1.size();
int l2=s2.size();
int L=l1;
for(int i=0;i<l1;i++)a[i]=s1[l1-1-i]-'0';
for(int i=0;i<l2;i++)b[i]=s2[l2-1-i]-'0';
for(int i=0;i<l2;i++){
for(int j=0;j<l1;j++){
c[i][j]+=b[i]*a[j];
c[i][j+1]+=c[i][j]/10;
c[i][j]%=10;
}
for(int k=l1;k>=0;k--){
c[i][k+i]=c[i][k];
}
for(int m=0;m<i;m++){
c[i][m]=0;
}
}
int j=l1+l2+2;
while(c[l2-1][j]==0){
j--;
}
L=j+1;
for(int i=0;i<L;i++){
for(int j=0;j<l2;j++){
res[i]+=c[j][i];
}
res[i+1]+=res[i]/10;
res[i]%=10;
}
for(int i=L-1;i>=0;i--){
str+=to_string(res[i]);
}
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
memset(res,0,sizeof(res));
return str;
}
string add(string s1,string s2){
str="";
int l1=s1.size();
int l2=s2.size();
for(int i=0;i<l1;i++)apple[i]=s1[l1-1-i]-'0';
for(int i=0;i<l2;i++)boy[i]=s2[l2-1-i]-'0';
int L=l1>l2?l1:l2;
for(int i=0;i<L;i++){
car[i]+=apple[i]+boy[i];
car[i+1]=car[i]/10;
car[i]%=10;
}
if(car[L]==1)L++;
for(int i=L-1;i>=0;i--){
str+=to_string(car[i]);
}
memset(apple,0,sizeof(apple));
memset(boy,0,sizeof(boy));
memset(car,0,sizeof(car));
return str;
}
string fact(int n){
if(n==1)return "1";
return mul(to_string(n),fact(n-1));
}
by Henry2012 @ 2023-07-07 20:44:36
二维数组太大了
by zfj123456 @ 2023-07-07 20:55:58
你int[1e4][1e4]的话,占内存是10000100004=400MB超了 @54dashabi
by zfj123456 @ 2023-07-07 20:56:29
@zfj123456 乘号没打上,能懂就行
by 54dashabi @ 2023-07-07 23:52:19
哦哦明白了,谢谢大家!