最后一个通过不了,我的输出里面都有减号了

P1009 [NOIP1998 普及组] 阶乘之和

fareless @ 2022-07-20 10:45:24

#include<iostream>
#include<vector>

using namespace std;

vector<long long> mul1(vector<long long> &A, int b){
    vector<long long> C;
    int t = 0;
    for (int i = 0; i < A.size(); i++){
        t += A[i] * b;
        C.push_back(t % 10);
        t /= 10;
    }
    if (t) C.push_back(t);
    return C;
}

vector<long long> mul(int n){
    vector<long long> A;
    A.push_back(n%10);
    if(n/10) A.push_back(n/10);
    int k=n-1;
    for(int i=k;i>=1;i--){
        A=mul1(A,i);
    }
    return A;
}

vector<long long> add(vector<long long> &A, vector<long long> &B){
    if (B.size() > A.size())
        return add(B, A);
    vector<long long> C;
    int t = 0;
    for (int i = 0; i < A.size(); i++){
        t += A[i];
        if (i < B.size())
            t += B[i];
        C.push_back(t % 10);
        t /= 10;
    }
    if (t) C.push_back(t);
    return C;
}

int main(){
    int n;
    cin>>n;
    auto A=mul(n);
    vector<long long> B;
    for(int i=n-1;i>=1;i--){
        B=mul(i);
        A=add(A,B);
    }
    while (A.size() > 1 && A.back() == 0) A.pop_back();//去除前导0
    for(int i=A.size()-1;i>=0;i--){
        cout<<A[i];
    }
    cout<<endl;
    system("pause");
    return 0;
}

by Tzj123_ORZ @ 2022-07-23 09:47:56

输出有负号应该是爆数据范围了


by hyc12528 @ 2022-08-30 22:25:10

高精


|