【P1009】階乘之和(高精求階乘之和)


震驚!數組多開大一個10經慘遭TLE!
數組大小爲1000010時100跑的慢死,可是爲100010時秒殺!
到底是%!¥#仍是@¥#)

題目:
ios

https://www.luogu.org/problem/show?pid=1009web


跳過胡扯咱們來看一下如何實現【一本正經】:
數組

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
#define RI register int
const int sz = 100010;
inline void read(int &x)
{
    x=0;bool fl=0;
    char c=getchar();
    while(c<'0'||c>'9')
    {
  
  
  

 if(c=='-') fl=1;c=getchar();}
    while(c>='0'&&c<='9')
    {x=x*10+c-'0';c=getchar();}
    if(fl) x*=-1;
}
inline void write(int x)
{
    if(x<0) putchar('-'),x*=-1;
    if(x/10) write(x/10);
    putchar(x%10+'0');
}
int he[sz],num[sz],s[sz],n;
inline void add()
{
    memset(he,0,sizeof(he));
    he[0]=max(s[0],num[0]);
    for(RI i=1;i<=he[0];++i)
    {
        he[i]+=s[i]+num[i];
        he[i+1]=he[i]/10;
        he[i]%=10;
    }
    while(he[ he[0]+1 ]>0)
    {
        he[ he[0]+2 ]=he[ he[0]+1 ]/10;
        he[ he[0]+1 ]%=10;
        he[0]++;
    }
    s[0]=he[0];
    for(RI i=1;i<=he[0];++i)
        s[i]=he[i];
}
int main()
{
    read(n);
    num[0]=1,num[1]=1;
    s[0]=1,s[0]=0;
    for(RI k=1;k<=n;++k)
    {
        memset(he,0,sizeof(he));
        he[0]=num[0];
        for(RI i=1;i<=num[0];++i)
        {
            he[i]+=num[i]*k;
            he[i+1]=he[i]/10;
            he[i]%=10;
        }
        while(he[ he[0]+1 ]>0)
        {
            he[ he[0]+2 ]=he[ he[0]+1 ]/10;
            he[ he[0]+1 ]%=10;
            he[0]++;
        }
        for(RI i=1;i<=he[0];++i)
            num[i]=he[i];
        num[0]=he[0];
        add();
    }
    for(RI i=s[0];i>=1;--i)
        write(s[i]);
    puts("");
    return 0;
}


s[0]和num[0]表示兩個數組的長度
s表示答案,num表示階乘,先算出階乘,放在num裏,再把s和它相加,更新s
主函數中初始化完畢後是高精乘法,在乘法中完成階乘運算,用add進行高精加法表示s=s+a
處理好中間的進位後輸出
svg