50分,没学高精度

P1009 [NOIP1998 普及组] 阶乘之和

guoyanwei120223 @ 2024-08-10 16:35:58

警示后人:不要这样写

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

by mayike @ 2024-08-10 16:41:43

@guoyanwei120223 你这个帖子貌似没有任何意义。50!谁不知道会爆long long?


by guoyanwei120223 @ 2024-08-10 16:45:37

@mayike 也不能这样写

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

by guoyanwei120223 @ 2024-08-10 16:50:10

@mayike 有人会不看数据的


by mayike @ 2024-08-10 16:51:16

@guoyanwei120223 是的

https://www.luogu.com.cn/record/list?pid=P1009&user=1039406


by guoyanwei120223 @ 2024-08-10 17:14:22

@mayike 渍渍渍


by PengPeng2 @ 2024-08-11 11:14:38

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

这样写才对一半,也不知道出了什么错


by longjinghui @ 2024-08-12 21:36:10

@PengPeng2 经计算,50的阶乘=8.407742111050573257248677738215e+62
使用unsigned long long也是会爆的


by guoyanwei120223 @ 2024-08-13 18:03:00

@PengPeng2 这道题要用高精度


by PengPeng2 @ 2024-08-15 18:06:17

@longjinghui 那怎么办


by RedWen_shuo @ 2024-08-16 07:28:37

int128 能 过 掉 第 三 个 点 ( QWQ )

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int Mo = 1e9 + 7;
vector<int> ans;
void write(unsigned __int128 x){
    if (x == 0) return;
    int h = x % 10;
    ans.push_back(h);
    write(x / 10);
}

int main(){

    ll n;
    unsigned __int128 sum1 = 1;
    unsigned __int128 sum2 = 0;
    cin >> n;
    for (unsigned __int128 i = 1;i <= n;i++){
        sum1 = 1;
        for (unsigned __int128 j = 1;j <= i;j++){
            sum1 *= j;
//          sum1 %= Mo;
        }
        sum2 += sum1;
//      sum2 %= Mo;
    }

    write(sum2);
    for (int i = ans.size() - 1; i >= 0; i --){
        printf("%d", ans[i]);
    }
    return 0;
}

| 下一页