求助

P1009 [NOIP1998 普及组] 阶乘之和

LH3223chen @ 2024-08-26 08:46:41

#include<iostream>
using namespace std;
int main()
{
    unsigned long long n, S=0,s=1;
    cin >> n;
    for (int a = 1; a <= n; a++)
    {
        for (int b = 1; b <=a; b++)
        {
            s *= b;

        }
        S += s;
        s = 1;
    }

    cout << S;
    return 0;
}

by wujunxi206 @ 2024-08-26 09:06:40

@LH3223chen 要用高精度


by wujunxi206 @ 2024-08-26 09:06:59

#include<iostream>
using namespace std;
int a[10005],b[10005];
int la=1,lb=1;
int jc(int n){
    int x=0;
    int i;
    for(i=1;i<=la;i++){
        a[i]=n*a[i];
    }
    for(i=1;i<=la;i++){
        a[i+1]+=a[i]/1000;
        a[i]%=1000;
    }
    if(a[i]>0){
        while(a[i]>1000){
            a[i+1]+=a[i]/1000;
            a[i]%=1000;
            i++;
        }
        la=i;
    }
    while(a[la]==0&&la>1){
        la--;
    }
    return 0;
}
int lj(){
    int k=0;
    lb=1;
    while(lb<=la){
        b[lb]=a[lb]+b[lb]+k;
        k=b[lb]/1000;
        b[lb]%=1000;
        lb++;
    }
    while(a[lb]==0&&lb>1){
        lb--;
    }
    return 0;
}
int main(){
    int n;
    cin>>n;
    a[1]=1;
    for(int i=1;i<=n;i++){
        jc(i);
        lj();
    }
    cout<<b[lb];
    for(int i=lb-1;i>=1;i--){
        printf("%.03d",b[i]);
    }
    return 0;
}

by wujunxi206 @ 2024-08-26 09:07:18

@LH3223chen 求关


by zhangkaishuo666 @ 2024-08-27 19:01:07

#include<bits/stdc++.h>
int main()
{
    int i,A[1005]={0},B[1005]={0},n,j;
    scanf("%d", &n);
    A[0]=B[0]=1;
    for (i=2;i<=n;i++){
        for (j=0;j<100;j++)
            B[j]*=i;
        for (j=0;j<100;j++)
            if (B[j]>9){
                B[j+1] += B[j]/10;
                B[j]%=10;
            }
        for (j=0;j<100;j++){
            A[j]+=B[j];
            if (A[j]>9) {
                A[j+1] += A[j]/10;
                A[j]%=10;
            }
        }
    }
    for (i=100;i>=0&&A[i]==0;i--);
    for (j=i;j>=0;j--) printf("%d", A[j]);
    return 0;
}

|