SocietyNiu 的博客

SocietyNiu 的博客

题解 P1009 【阶乘之和】

posted on 2018-10-31 15:08:16 | under 题解 |

想获得更加的阅读体验?

写python代码的,难道你们不知道联赛不支持python?

写打表的,你们的generator在哪里?

python自带高精度,可是联赛中不支持
c++联赛中允许使用,但是得自己写高精度
这道题数据范围是 $n≤50$
是否能用一种方法,集聚两家的智慧
当然有

打表

具体思路

先用python打表,然后以字符串形式存入c++代码中

实现

python代码

f=open(r'point.txt','w') #存入文件
sum=1
i=2
last=1
n=int(input())
while i<=n:
    last*=i
    sum+=last
    i+=1                 #主函数
    f.write('"')         #上引号
    f.write(str(sum))   #具体值
    f.write('",')       #下引号与逗号
f.close               #关闭文件写入

存入cpp后代码

#include<iostream>
using namespace std;
string a[60]={"0","1","3","9","33","153","873","5913","46233","409113","4037913","43954713","522956313","6749977113","93928268313","1401602636313","22324392524313","378011820620313","6780385526348313","128425485935180313","2561327494111820313","53652269665821260313","1177652997443428940313","27029669736328405580313","647478071469567844940313","16158688114800553828940313","419450149241406189412940313","11308319599659758350180940313","316196664211373618851684940313","9157958657951075573395300940313","274410818470142134209703780940313","8497249472648064951935266660940313","271628086406341595119153278820940313","8954945705218228090637347680100940313","304187744744822368938255957323620940313","10637335711130967298604907294846820940313","382630662501032184766604355445682020940313","14146383753727377231082583937026584420940313","537169001220328488991089808037100875620940313","20935051082417771847631371547939998232420940313","836850334330315506193242641144055892504420940313","34289376947494122614363304694584807557656420940313","1439295494700374021157505910939096377494040420940313","61854558558074209658512637979453093884758552420940313","2720126133346522977702138448994068984204397080420940313","122342346998826717539665299944651784048588130840420940313","5624964506810915667389970728744906677010239883800420940313","264248206017979096310354325882356886646207872272920420940313","12678163798554051767172643373255731925167694226950680420940313","620960027832821612639424806694551108812720525606160920420940313","31035053229546199656252032972759319953190362094566672920420940313"};
int main()
{
    int n;
    cin>>n;
    cout<<a[n];
}

在考场上,python可以作为工具使用,但不能作为源文件提交,因此用python打表,用c++提交,岂不美哉?

正常高精程序

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int a[2000];
int b[2000];
int c[2000];
int sum[2000];
void pplus(int *a,int *c)
{
    int jw=0;
    for(int i=1;i<=1000;i++)
    {
        c[i]+=a[i]+jw;
        jw=c[i]/10;
        c[i]%=10;
    }
}
void cheng(int *a,int c)
{
    int jw=0;
    for(int i=1;i<=1000;i++)
    {
        a[i]=a[i]*c+jw;
        jw=a[i]/10;
        a[i]%=10;
    }
}
int main()
{
    int n;
    cin>>n;
    a[1]=1;
    for(int i=1;i<=n;i++)
    {
        cheng(a,i);
        pplus(a,c);
    }
    bool flag=0;
    for(int i=1000;i>=1;i--)
    {
        if(c[i]!=0) flag=1;
        if(flag) cout<<c[i];
    }
}

人生苦短,我用py