正常做法全TLE

P1009 [NOIP1998 普及组] 阶乘之和

zbyzhaoboyu0 @ 2024-07-27 13:18:26

我用的是正常的双重循环做法但是全部超时

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

by haimingbei @ 2024-07-27 13:20:05

@zbyzhaoboyu0 大哥,这题是高精度!


by haimingbei @ 2024-07-27 13:21:15

@zbyzhaoboyu0 代码(AC,求关)

/*
5、P1009 [NOIP1998 普及组] 阶乘之和
*/
#include<bits/stdc++.h>
using namespace std;
int n,s[105],a[105];
int main(){
    cin>>n;
    int len,lens=1;
    for(int i=1;i<=n;i++){
        len=1,a[1]=1;
        for(int j=1;j<=i;j++){
            int jw=0,t;
            for(int k=1;k<=len;k++){
                t=a[k]*j+jw;
                jw=t/10;
                a[k]=t%10;
            }
            while(jw>0){
                a[++len]=jw%10;
                jw/=10;
            }
        }
        int t,jw=0;
        lens=max(lens,len)+1;
        for(int j=1;j<=lens;j++){
            t=a[j]+s[j]+jw;
            s[j]=t%10;
            jw=t/10;
        }
        while(s[lens]==0)lens--;
    }
    for(int i=lens;i>=1;i--)cout<<s[i];
    return 0;
}

by King_and_Grey @ 2024-07-27 13:43:48

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1000;

struct Bigint {

    int len, a[maxn];

    Bigint(int x = 0) {

        memset(a, 0, sizeof(a));

        for (len = 1; x; len++)

            a[len] = x % 10, x /= 10;

        len--;

    }

    int &operator[](int i) {

        return a[i];

    }

    void flatten(int L) {

        len = L;

        for (int i = 1; i <= len; i++)

            a[i + 1] += a[i] / 10, a[i] %= 10;

        for (; !a[len]; )

            len--;

    }

    void print() {

        for (int i = max(len, 1); i >= 1; i--)

            printf("%d", a[i]);

    }

};

Bigint operator+(Bigint a, Bigint b) {//重载加号

    Bigint c;

    int len = max(a.len, b.len);

    for (int i = 1; i <= len; i++)

        c[i] += a[i] + b[i];

    c.flatten(len + 1);

    return c;

}

Bigint operator*(Bigint a, int b) {//重载乘号

    Bigint c;

    int len = a.len;

    for (int i = 1; i <= len; i++)

        c[i] = a[i] * b;

    c.flatten(len + 11);

    return c;

}

int main() {

    Bigint ans(0),fac(1);

    int m;

    cin >> m;

    for(int i = 1; i <= m; i++) {

        fac = fac * i;

        ans = ans + fac;

    }

    ans.print();

    return 0;

}

C++高精度模板。


by Jokersheng @ 2024-07-31 23:11:09

阿哲

你为什么是nt j=i;i>=1;i--?

你的i一直等于1

#include<iostream>
using namespace std;
long long n,sum;
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) {
        int cnt = 1;
        for(int j = 1;j <= i;j++) cnt*=j;
        sum+=cnt;
    }
    cout << sum;
    return 0;
}

求关,话说验证码sc03什么意思[doge]


by Jokersheng @ 2024-07-31 23:11:26

阿哲

你为什么是nt j=i;i>=1;i--?

你的i一直等于1

#include<iostream>
using namespace std;
long long n,sum;
int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) {
        int cnt = 1;
        for(int j = 1;j <= i;j++) cnt*=j;
        sum+=cnt;
    }
    cout << sum;
    return 0;
}

求关[doge]


by Jokersheng @ 2024-07-31 23:12:51

@zbyzhaoboyu0 m还要初始值为1(特指第一个for内)


by pangpanghu @ 2024-08-02 21:29:51

@shengshengzqs
?这题是高精。。。
QNT2


by pangpanghu @ 2024-08-02 21:34:46

@shengshengzqs

50!=30414100000000000000000000000000000000000000000000000000000000000

|