75求助,玄关

P1009 [NOIP1998 普及组] 阶乘之和

jinitaimei11 @ 2025-01-04 13:42:07

改AC就关注

记录

#include <bits/stdc++.h>
using namespace std;
void print(__int128 x){
    if (x < 0){
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
__int128 read(){
    __int128 x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9'){
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9'){
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
__int128 jiecheng(__int128 shu){
    __int128 sum = 1;
    for(__int128 i = 2; i <= shu; i++){
        sum *= i;
    }
    return sum;
}

int main(){
    __int128 a, sum = 0;
    a = read();
    for(__int128 i = 1; i <= a; i++){
        sum += jiecheng(i);
    }

    print(sum);
    return 0;
}

by craftmine @ 2025-01-04 13:48:46

@jinitaimei11...int128会爆


by craftmine @ 2025-01-04 13:49:43

肝了一年的高精度:

#include<bits/stdc++.h>
using namespace std;
struct vli{
    int l,a[10001];
    vli(int x=0){
        memset(a,0,10001);
        for(l=1;x;l++){
            a[l]=x%10;
            x/=10;
        }
        l--;
    }
    int &operator[](int i){
        return a[i];
    }
    void fla(int s){
        l=s;
        for(int i=1;i<=l;i++){
            a[i+1]+=a[i]/10;
            a[i]%=10;
        }
        while(!a[l]){
            l--;
        }
    }
    void print(){
        for(int i=max(l,1);i>=1;i--){
            cout<<a[i];
        }
    }
};
vli operator+(vli a,vli b){
    vli c;
    int l=max(a.l,b.l);
    for(int i=1;i<=l;i++){
        c[i]+=a[i]+b[i];
    }
    c.fla(l+1);
    return c;
}
vli operator*(vli a,int b){
    vli c;
    int l=a.l;
    for(int i=1;i<=l;i++){
        c[i]+=a[i]*b;
    }
    c.fla(l+11);
    return c;
}
int main(){
    int n;
    vli ans(0),num(1);
    cin>>n;
    for(int i=1;i<=n;i++){
        num=num*i;
        ans=ans+num;
    }
    ans.print();
    return 0;
}

by craftmine @ 2025-01-04 13:50:54

你就是__int256都要爆的


by craftmine @ 2025-01-04 13:58:16

\log a*b=\log a+\log b\log_250!=\sum^{50}_{i=1}\log_2i


by craftmine @ 2025-01-04 14:00:34

即最后结果大于2^{200}


by jinitaimei11 @ 2025-01-05 11:41:00

@craftmine谢谢大佬,已关


|