75求条

P1009 [NOIP1998 普及组] 阶乘之和

xuyi20110411 @ 2024-10-06 08:06:24


#include<bits/stdc++.h>
using namespace std;
int n,k;
string s,k1;
string bg(string a,string b){
    int a1[55]={0},b1[55]={0},ci,c[2010011]={0};
    string d="";
    for(int i=a.size()-1;i>=0;i--)a1[a.size()-i]=a[i]-'0';
    for(int i=b.size()-1;i>=0;i--)b1[b.size()-i]=b[i]-'0';
    for(int i=1;i<=a.size();i++){
        ci=0;
        for(int j=1;j<=b.size();j++){
            c[i+j-1]=a1[i]*b1[j]+ci+c[i+j-1];
            ci=c[i+j-1]/10;
            c[i+j-1]%=10;
        }
        c[i+b.size()]=ci;
    }
    int lenc=a.size()+b.size();
    while(lenc>1&&c[lenc]==0)lenc--;
    for(int i=lenc;i>=1;i--)d+=c[i]+'0';
    return d;
}
string x(int a){
    if(a==1)return "1";
    else{
        string d="",e="";
        int m=a;
        while(m){
            d+=(char)((m%10)+'0');
            m/=10;
        }
        for(int i=d.size()-1;i>=0;i--)e+=d[i];
        return bg(e,x(a-1));    
    }
}
void m(string a){
    int a1[55]={0},b1[55]={0},c1[2000111]={0};
    int lena=a.size();
    int lenb=k1.size();
    for(int i=1;i<=lena;i++)a1[i]=a[lena-i]-'0';
    for(int i=1;i<=lenb;i++)b1[i]=k1[lenb-i]-'0';
    int l=1,x=0;
    while(l<=lena||l<=lenb){
        c1[l]+=a1[l]+b1[l];
        x=c1[l]/10;
        c1[l+1]+=x; 
        c1[l]%=10;
        l++;
    }
    c1[l]=x;
    while(c1[l]==0&&l>1)l--;
    string q="";
    for(int i=l;i>=1;i--)q+=c1[i]+'0';
    k1=q;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        string s=x(i);
        m(s);
    }
    cout<<k1;
    return 0;
}

by zhanghx0219 @ 2024-10-06 08:27:19

数组开小了吧


|