全错!这高精度怎么简单算 ?

P1009 [NOIP1998 普及组] 阶乘之和

Yu09217777 @ 2022-10-07 11:32:59

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,sum,t;
    cin >> n;
    for(int i = 1;i <= n;i++){
        for(int j = i;j >= 1;j--){
            t *= j;
        }
        sum += t;
        t = 1;  
    }
    cout << sum;
    return 0;
}

by LiaoYF @ 2022-10-07 11:34:46

高精度就用高精度啊


by 喵仔牛奶 @ 2022-10-07 11:35:18

@Yu09217777 如果只用 C++ 的话没有办法简单算,只能学习一下高精度了(。・ω・。)


by Lemon_X @ 2022-10-07 11:35:37

你这个普通的当然A不了高精……甚至连long long都没开


by LiaoYF @ 2022-10-07 11:35:52

@Yu09217777 帮你改到50分record

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,sum=0,t=1;
    cin >> n;
    for(int i = 1;i <= n;i++){
        for(int j = i;j >= 1;j--){
            t *= j;
        }
        sum += t;
        t = 1;  
    }
    cout << sum;
    return 0;
}

by 喵仔牛奶 @ 2022-10-07 11:38:55

@Yu09217777

改到了 75 分,加油!

#include <bits/stdc++.h>
using namespace std;
void write(__int128 x){
    if(x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}
int main(){
    int n;
    __int128 sum=0,t=1;
    cin >> n;
    for(int i = 1;i <= n;i++){
        for(int j = i;j >= 1;j--){
            t *= j;
        }
        sum += t;
        t = 1;  
    }
    write(sum);
    return 0;
}

by __er @ 2022-10-07 11:39:18

n=eval(input())     
ans=0
for i in range(1,n+1):
    sum=1
    for j in range(1,i+1):
        sum*=j
    ans+=sum
print(ans)

(bushi


by __er @ 2022-10-07 11:39:40

@Yu09217777 改到100分


by hjsxhst2022 @ 2022-10-07 11:46:22

@Yu09217777 用python水啊


by hjsxhst2022 @ 2022-10-07 11:46:56

@Yu09217777 long double都过不去你想用int就过去?


by yh2022mayu @ 2022-10-07 11:48:49

python不用高精度哦

vector<int> add(vector<int> &a, vector<int> &b){
    vector<int> c;
    int t = 0;
    for(int i = 0; i < a.size() || i < b.size(); i++){
        if(i < a.size()) t += a[i];
        if(i < b.size()) t += b[i];
        c.push_back(t % 10);
        t /= 10;
    }
    c.push_back(t);
    while(c.size() > 1 && c[c.size() - 1] == 0) c.pop_back();
    return c;
}

高精加法


| 下一页