警示后人(这题竟然没法用黑科技!)

P1009 [NOIP1998 普及组] 阶乘之和

fengyongrui @ 2023-03-19 11:52:16

我本想用黑科技(__int128)的:

#include<bits/stdc++.h>
using namespace std;
long long n,ans,a[999999];
__int128 x=1,ans1;
inline __int128 jc(unsigned long long n){
    x=1;
    for(int i=n;i>=1;i--) x*=i;
    return x;
}
inline void print(){
    int cnt=0,x=0;
    while(ans1>0){
        a[++x]=ans1%10;
        ans1/=10;
        cnt++;
    }
    for(int i=cnt;i>=1;i--) cout<<a[i];
    return;
}
int main(){
    __int128 i,j;
    cin>>n;
    for(i=1;i<=n;i++) ans1+=jc(i);
    print();
    return 0;
}

pa(75分)

这告诉我们:一定要小心:n<=50!


by codejiahui @ 2023-03-19 12:04:29

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring> 
using namespace std;
int ans[1010],a[1010];
void printBIG(int* a)//高精输出
{
    for (int i = a[0];i >= 1;i--)
        cout << a[i];
    cout << endl;
    return;
}
void mulBIG(int* x,int y,int* z)//高精小乘大
{
    z[0] = x[0];
    for (int i = 1;i <= z[0];i++)
        z[i] = x[i] * y;
    for (int i = 1;i <= z[0];i++)
    {
        z[i + 1] += z[i] / 10;
        z[i] %= 10;
        if (z[z[0] + 1] > 0) z[0]++;
    }
}
void addBIG(int* x,int* y,int* z)
{
    z[0] = max(x[0],y[0]);
    for (int i = 1;i <= z[0];i++)
        z[i] = x[i] + y[i];
    for (int i = 1;i <= z[0];i++)
    {
        z[i + 1] += z[i] / 10;
        z[i] %= 10;
        if (z[z[0] + 1] > 0) z[0]++;
    }
}
int main()
{
    int n;
    cin >> n;
    for (int i = 1;i <= n;i++)
    {
        memset(a,0,sizeof(a));
        a[0] = a[1] = 1;
        for (int j = 1;j <= i;j++)
            mulBIG(a,j,a);
        addBIG(ans,a,ans);
    }
    printBIG(ans);
    return 0;
}

by Shadow_T @ 2023-03-19 12:13:32

__int128最大差不多到10^38


by fengyongrui @ 2023-03-20 20:56:34

@tcy01 确实,高精数据小店还行,大了就不行,还得乖乖用高精


|