50分,用了高精度,c++

P1009 [NOIP1998 普及组] 阶乘之和

IAKpoki @ 2024-04-17 20:42:01

有乱码;

#include<bits/stdc++.h>
using namespace std;
string arr[60];
int arr1[999];
int arr2[999];
int arr3[9999];
string sum[60];
string cheng(string a,string b){
    int len1=a.size();
    int len2=b.size();
    for(int i=0;i<len1;i++){
        arr1[i]=a[len1-1-i]-'0';
    }
    for(int i=0;i<len2;i++){
        arr2[i]=b[len2-1-i]-'0';
    }
    int len=len1+len2-1;
    for(int i=0;i<len1;i++){
        for(int j=0;j<len2;j++){
            arr3[i+j]+=arr1[i]*arr2[j];
        }
    } 
    for(int i=0;i<len;i++){

        arr3[i+1]+=arr3[i]/10;
        arr3[i]%=10;

    }
    if(arr3[len]){
            len++;
    }
    string s="";
    for(int i=len-1;i>=0;i--){

        s=s+(char)(arr3[i]+'0');
    }
    for(int i=0;i<=len;i++){
        arr1[i]=0;
        arr2[i]=0;
        arr3[i]=0;
    }
    return s;
}
int sum1[1000];
int sum2[999];
string jia(string a,string b){
    int len1=a.size();
    for(int i=0;i<len1;i++){
        sum1[i]=a[len1-i-1]-'0';
    }
    int len2=b.size();
    for(int i=0;i<len2;i++){
        sum2[i]=b[len2-i-1]-'0';
    }
    int len=max(len1,len2);
    for(int i=0;i<len;i++){
        sum1[i]+=sum2[i];
        sum1[i+1]+=sum1[i]/10;
        sum1[i]%=10;
    }
    if(sum1[len]){
        len++;
    }
    string s="";
    for(int i=len-1;i>=0;i--){
        s=s+(char)(sum1[i]+'0');
    }
    for(int i=0;i<=len;i++){
        sum1[i]=0;
        sum2[i]=0;

    }
    return s;
}
int main(){
    arr[1]="1";
    sum[1]="1";
    int n;
    cin>>n;
    for(int i=2;i<=n;i++){
        string i2="";
        i2+=(i+'0');
        arr[i]+=cheng(arr[i-1],i2);
        sum[i]=jia(sum[i-1],arr[i]);
    }
    cout<<sum[n];
    return 0;
} 

^_^


by IAKpoki @ 2024-04-17 20:47:17

https://www.luogu.com.cn/discuss/808890


by Crab_Tang @ 2024-04-17 20:57:45

@Simiao 高精度错了。


by Crab_Tang @ 2024-04-17 20:58:48

@Simiao 而且貌似没有去除前导零


|