50分c++

P1009 [NOIP1998 普及组] 阶乘之和

F_oR_es_T @ 2022-07-16 19:29:36

求大佬


#include<iostream>
using namespace std;
unsigned long long jie(int a){
    unsigned long long b=1;
    for(long long i=1;i<=a;i++){
        b*=i;
    }
    return b;
}
int main(){
    long long n;
    cin>>n;
    unsigned long long h=0;
    for(long long i=1;i<=n;i++){
        unsigned long long m=jie(i);
        h+=m;
    }
    cout<<h;
    return 0;
}

by LJKX @ 2022-07-16 19:30:14

高精


by Escapism @ 2022-07-16 19:30:23

@start_dream 你看看范围吧


by tangrunxi @ 2022-07-16 19:30:55

@start_dream 这题要写高精度,unsigned long long也不行。


by Escapism @ 2022-07-16 19:31:22

@start_dream 1 \leq 50,这个就算用无符号长整形也都爆啊

建议学高精


by F_oR_es_T @ 2022-07-16 19:56:22

没学过啊,高精咋写


by luye167 @ 2022-07-18 10:29:29

@start_dream

#include <iostream>
#include <cstring>
using namespace std;

int main(){
    int n,i,j,k,a[1000],s[1000];
    memset(a,0,1000);
    memset(s,0,1000);

    cin>>n;
    a[0]=1;
    a[1]=1;
    s[0]=1;
    s[1]=0;
    for(i=1;i<=n;i++) {
        k=0;
        for(j=1;j<=a[0];j++){
            a[j] = a[j]*i+k;
            k=a[j]/10;
            a[j] = a[j]%10;
        }
        a[j] =k;
        while(a[j]>0) {
            a[j+1] = a[j]/10;
            a[j] = a[j]%10;
            j++;
            a[0]++;
        }

        for(j=1;j<=a[0];j++){
            s[j] += a[j];
            if(s[j]>9) {
                s[j+1]++;
                s[j] = s[j]%10;
            }
        }
        s[0] = max(s[j]>0?(a[0]+1):a[0],s[s[0]+1]>0?(s[0]+1):s[0]);         
    }

    for(i=s[0];i>0;i--) {
        cout<<s[i];
    }
    return 0;
}

by tushu @ 2022-07-19 17:01:48

@start_dream 高精度就是你需要用数组来模拟竖式乘法,然后用数组把每一位输出出来


by F_oR_es_T @ 2022-07-20 09:04:10

@tushu谢谢昨天刚学了高精


|