高精度25分求助

P1009 [NOIP1998 普及组] 阶乘之和

Ming_Yu @ 2022-10-24 10:03:04

哪里寄了呢?

#include <bits/stdc++.h>
using namespace std;
int n;
string ans="1";
int a[110000],b[110001],c[101000];
string jia(string s1,string s2)
{
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    int len1=s1.size();
    int len2=s2.size();
    int len3=max(len1,len2)+1;
    for(int i=0; i<len1; i++)
        a[i]=s1[len1-i-1]-'0';
    for(int i=0; i<len2; i++)
        b[i]=s2[len2-i-1]-'0';
    for(int i=0; i<len3; i++)
    {
        c[i]+=a[i]+b[i];
    }
    for(int i=0; i<len3; i++)
    {
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    int k=len3-1;
    string s3;
    while(c[k]==0&&k>=1) k--;
    for(int i=k; i>=0; i--)
        s3+=c[i]+'0';
    return s3;
}
string cheng(string s1,string s2)
{
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    int len1=s1.size();
    int len2=s2.size();
    int len3=(len1+len2);
    for(int i=0; i<len1; i++)
        a[i]=s1[len1-i-1]-'0';
    for(int i=0; i<len2; i++)
        b[i]=s2[len2-i-1]-'0';
    for(int i=0; i<len1; i++)
    {
        for(int j=0; j<len2; j++)
        {
            c[i+j]+=a[i]*b[j];
        }
    }
    for(int i=0; i<len3; i++)
    {
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    int k=len3-1;
    string s3;
    while(c[k]==0&&k>=1) k--;
    for(int i=k; i>=0; i--)
        s3+=c[i]+'0';
    return s3;
}
string jc(int x)
{
    string res="1";
    for(int i=2; i<=x; i++)
    {
        int p=i;
        string y="";
        while(p)
        {
            y=y+char(p%10+'0');
            p/=10;
        }
        res=cheng(res,y);
    }
    return res;
}
int main()
{
    cin>>n;
    for(int i=2; i<=n; i++)
    {
        ans=jia(ans,jc(i));
//      cout<<ans<<endl;
    }

    cout<<ans;
    return 0;
}

by huangronuxuan @ 2022-10-24 10:37:04

其实可以打表的


by Ming_Yu @ 2022-10-24 11:12:08

@huangronuxuan QAQ


|