悬~~关~~求~~助

P1009 [NOIP1998 普及组] 阶乘之和

K_func @ 2024-01-31 18:08:54

#include <bits/stdc++.h>
using namespace std;
struct Hugeint{
    int len,digits[32768];
    Hugeint(int x = 0){
        memset(digits,0,sizeof(digits));
        for(len = 1;x;len++){
            digits[len] = x%10;
            x/=10;
        }
        len--;
    }
    int &operator[](int i){
        return digits[i];
    }
    void carry(int l){
        len = l;
        for(int i=1;i<=len;i++){
            digits[i+1]+=digits[i]/10,digits[i]%=10;
        }
        for(;!digits[len];){
            len--;
        }
    }
    void output(){
        for(int i=max(len,1);i>=1;i--){
            printf("%d",digits[i]);
        }
    }
    Hugeint operator+(Hugeint num){
        Hugeint temp;
        len = max(num.len,len);
        for(int i=1;i<=len;i++){
            int tmp = digits[i]+num[i];
            temp[i] = tmp%10;
            temp[i+1] += tmp/10;
        }
        return temp;
    }
    Hugeint operator*(int num){
        Hugeint tmp;
        for(int i=1;i<=len;i++){
            tmp[i]+=digits[i]*num;
        }
        tmp.carry(len+1);
        return tmp;
    }
};
Hugeint ans(0),a2333(1);
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        a2333=a2333*i;
        ans=ans+a2333;
    }
    ans.output();
}

0pts


by _buzhidao_ @ 2024-01-31 18:15:59

@Jadejunxi

 #include<iostream>
using namespace std;
int a[1001],b[1001];
void add(){
    int t=0;
    for(int i=1000;i>=1;i--)
    {
        b[i]=b[i]+a[i]+t;
        t=b[i]/10;
        b[i]%=10;
    }
}
void cheng(int x){
    int t=0;
    for(int i=1000;i>=1;i--)
    {
        a[i]=a[i]*x+t;
        t=a[i]/10;
        a[i]%=10;
    }
}
void print(){
    int flag=0;
    for(int i=1;i<=1000;i++)
    {
        if(b[i]!=0) flag=1;
        if(flag) cout<<b[i];
    }
}
short n;
int main(){
    a[1000]=1;
    b[1000]=1;
    cin>>n;
    for(int i=2;i<=n;i++){
        cheng(i);
        add();
    }
    print();
    return 0;
}

by K_func @ 2024-01-31 18:20:27

@buzhidao thank you,已关


by fanjiayu666 @ 2024-01-31 18:52:25

@Jadejunxi 数据量很大,得用高精度算法!


|