title: P1009 阶乘之和 categories:


#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
int bor[52][10000];
int n;
int main(){
    bor[0][0]=1;
    bor[0][1]=1;
    bor[1][0]=1;
    bor[1][1]=1;
    cin>>n;
    for(int i=2;i<=n;i++){
        for(int r=1;r<=bor[i-1][0];r++){
            bor[i][r]=bor[i-1][r]*i;
        }
        bor[i][0]=bor[i-1][0];
        for(int k=1;k<=bor[i][0];k++){
            if(bor[i][k]>9){
                if(k==bor[i][0]){
                    bor[i][0]++;
                }
                bor[i][k+1]+=bor[i][k]/10;
                bor[i][k]%=10;
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int r=1;r<=bor[n][0];r++){
            bor[n+1][r]+=bor[i][r];
        }
    }
    bor[n+1][0]=bor[n][0];
    for(int k=1;k<=bor[n+1][0];k++){
        if(bor[n+1][k]>9){
            if(k==bor[n+1][0]){
                bor[n+1][0]++;
            }
            bor[n+1][k+1]+=bor[n+1][k]/10;
            bor[n+1][k]%=10;
        }
    }
    for(int i=bor[n+1][0];i>0;i--){
        cout<<bor[n+1][i];
    }
    cout<<endl;
    return 0;
}