TOT谁能理解一本通给我100洛谷给我50的痛苦

P1009 [NOIP1998 普及组] 阶乘之和

Ling34Huang21mXuan @ 2024-01-30 20:52:10


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

大佬们,请指点一二!!!

by xiaoshumiao @ 2024-01-30 20:52:57

@Ling34Huang21mXuan 这题要用高精度。


by Ling34Huang21mXuan @ 2024-01-30 21:12:12

@xiaoshumiao 才看到,谢谢


by qeyp @ 2024-01-30 21:12:36

我这个代码只有O(n)的时间复杂度,不知道为啥也没过```c

include <iostream>

using namespace std; int main() { long long n; cin>>n; long long cnt=n; while(n!=1) { n--; cnt++; cnt*=n; } cout<<cnt; return 0; }


by xiaoshumiao @ 2024-01-30 21:13:18

@heixinghongbai 都说了要用高精度。


by qeyp @ 2024-01-30 21:14:53


by swuster27 @ 2024-01-30 22:53:44


//高精度加法+高精度乘法
#include<iostream>
using namespace std;
static const int N = 100;
int a[N], b[N];
int main()
{
    int n, len;
    cin >> n;
    a[0] = 1; b[0] = 1;
    for (int i = 2; i <= n; i++)
    {
        for (int j = 0; j <N ; j++)
        {
            a[j] *= i;
        }
        for (int j = 0; j <N; j++)
        {
            a[j + 1] += a[j] / 10;
            a[j] %= 10;
        }
        for (len = 0; len <N; len++)
        {
            b[len] += a[len];
            b[len + 1] += b[len] / 10;
            b[len] %= 10;
        }
    }
    for (len=N; b[len] == 0; len--);
    for (int i = len; i >= 0; i--)
    {
        cout << b[i];
    }
    cout << endl;
    return 0;
}

by Ling34Huang21mXuan @ 2024-01-31 16:03:34

@swuster27 Thanks!


by fanjiayu666 @ 2024-01-31 18:54:21

@xiaoshumiao 同意楼上


|