75分求助

P1009 [NOIP1998 普及组] 阶乘之和

Maysoul @ 2023-03-31 11:26:13

下了测试点发现 n=48时

输出是

12678163798554051767172643373255731925167694226950680420940313

我的输出是

-5-4-90-5-8-8-7-1-10-30-1-3-5-9-7-6-9-6-1-5-7-4-9-7-6-3-1-5-4-7213-46

根本不知道为什么会输出负号

求大佬帮调

//2023/3/31
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e6+10;
int num,ans;
int jie[10000000];
int he[10000000];
int main()
{
    int n;
    int len=1,len1=1;
    int z=0,s=0;
    jie[0]=1;
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=i;j++)
        {
            for (int k=0;k<len;k++)
            {
                jie[k]*=j;
                jie[k]+=z;
                z=jie[k]/10;
                if(jie[k]>=10)
                {
                    jie[k]%=10;
                }

            }
            if(z>0)
            {
                jie[len]+=z;
                len++;
                z=0;
            }
        }
        len1=len;
        for (int j=0;j<len1;j++)
        {
            he[j]+=jie[j];
            he[j]+=s;
            s=he[j]/10;
            he[j]%=10;
        }
        if(s>0)
        {
            he[len1]+=s;
            len1++;
            s=0;
        }
        /*for (int i=len1-1;i>=0;i--)
        {
            cout<<he[i];
        }
        cout<<endl;*/

        for (int j=0;j<len;j++)
        {
            jie[j]=0;
        }
        jie[0]=1;
        len=1;
    }
    for (int i=len1-1;i>=0;i--)
    {
        cout<<he[i];
    }
    return 0;
}

by HuangTian @ 2023-03-31 12:20:03

开long long


by Maysoul @ 2023-03-31 13:55:41

@HuangTian 谢谢,头一回知道高精还需要写long long


by Maysoul @ 2023-03-31 14:04:56

@HuangTian 再问一下大佬,改了之后在一本通上依旧过不了,为什么?

http://ybt.ssoier.cn:8088/problem_show.php?pid=1173


by HuangTian @ 2023-04-01 08:57:05

一本通n<=100


by Maysoul @ 2023-04-01 09:29:26

@HuangTian 跟这个有什么关系吗


by HuangTian @ 2023-04-01 16:54:38

有,要开__int128


by Maysoul @ 2023-04-02 17:51:50

@HuangTian

开了__int128也没用啊

//2023/3/31
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN=1e6+10;
int num,ans;
__int128 jie[10000000];
__int128 he[10000000];
void print(__int128 x)
{
    if (x<0)
    {
        putchar('-');
        x=-x; 
    }
    if(x>9) print(x/10);
    putchar(x%10+'0');
}
__int128 read()
{
    __int128 f=1,w=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
        f=-1;
        ch=getchar();
    }
    while(ch<='9'&&ch>='0')
    {
        w=w*10+ch-'0';
        ch=getchar();
    }
    return f*w;
}
signed main()
{
    __int128 n;
    __int128 len=1,len1=1;
    __int128 z=0,s=0;
    jie[0]=1;
    n=read();
    for (__int128 i=1;i<=n;i++)
    {
        for (__int128 j=1;j<=i;j++)
        {
            for (__int128 k=0;k<len;k++)
            {
                jie[k]*=j;
                jie[k]+=z;
                z=jie[k]/10;
                if(jie[k]>=10)
                {
                    jie[k]%=10;
                }

            }
            if(z>0)
            {
                jie[len]+=z;
                len++;
                z=0;
            }
        }
        len1=len;
        for (__int128 j=0;j<len1;j++)
        {
            he[j]+=jie[j];
            he[j]+=s;
            s=he[j]/10;
            he[j]%=10;
        }
        if(s>0)
        {
            he[len1]+=s;
            len1++;
            s=0;
        }
        /*for (int i=len1-1;i>=0;i--)
        {
            cout<<he[i];
        }
        cout<<endl;*/

        for (__int128 j=0;j<len;j++)
        {
            jie[j]=0;
        }
        jie[0]=1;
        len=1;
    }
    for (__int128 i=len1-1;i>=0;i--)
    {
        print(he[i]);
    }
    return 0;
}

http://ybt.ssoier.cn:8088/statusx.php?runidx=22101757


by HuangTian @ 2023-04-05 08:28:13

高精可能写假了


|