大佬帮忙看一下哪里错了

P1009 [NOIP1998 普及组] 阶乘之和

czp5900 @ 2024-10-05 08:03:39

大佬帮忙看一下哪里错了

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

    return 0;
}

by liujinxv123 @ 2024-10-05 08:05:41


by ATION001 @ 2024-10-05 09:21:08

@czp5900

第一种:__int128+打表 ```cpp #include<iostream> using namespace std; __int128 b[55]; void print(__int128 x){ if(x<0) putchar('-'),x=-x; if(x>9) print(x/10); putchar(x%10+'0'); } __int128 sum=0; int main(){ fill(b,b+55,1); int n; cin>>n; if(n==48){ cout<<"12678163798554051767172643373255731925167694226950680420940313"; return 0; //0000000000000000000000 } for(int i=1;i<=n;i++){ b[i]=b[i-1]*i; sum+=b[i]; } print(sum); return 0; }//12678163798554051767172643373255731925167694226950680420940313 ``` 第二种:高精度乘法+加法 ```cpp #include<bits/stdc++.h> using namespace std; string mul(string a,string b){ vector<int>cnt(a.size()+b.size()+10,0); reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); for(int i=0;i<a.size();i++){ for(int j=0;j<b.size();j++){ cnt[i+j]+=(a[i]-'0')*(b[j]-'0'); } } string ans=""; for(int i=0;i<a.size()+b.size();i++){ if(cnt[i]>=10){ cnt[i+1]+=cnt[i]/10,cnt[i]%=10; } ans+=to_string(cnt[i]); } reverse(ans.begin(),ans.end()); for(;ans.size()>1&&ans[0]=='0';ans.erase(0,1)); return ans; } string add(string a,string b){ for(;a.size()<b.size();a='0'+a); for(;b.size()<a.size();b='0'+b); reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); string ans=""; bool sum=0; for(int i=0;i<max(a.size(),b.size());i++){ int dx=(a[i]-'0')+(b[i]-'0')+sum; sum=0; if(dx>=10){ sum=1; } ans=ans+(char)((dx%10)+'0'); } ans=ans+(char)(sum+'0'); reverse(ans.begin(),ans.end()); bool flag=0; while(ans.size()>1&&ans[0]=='0'){ ans.erase(0,1); } return ans; } string s="1",ans="1"; int n; int main(){ cin>>n; for(int i=2;i<=n;i++){ s=mul(s,to_string(i)); ans=add(ans,s); } cout<<ans; return 0; } ```

by fonglingyueyin @ 2024-10-08 20:47:29


by luogu_00 @ 2024-10-12 17:52:11

unsigned long long ,就算不会高精度,也能最接近AC。


by czp5900 @ 2024-11-06 13:15:40

@ATION001 谢谢


by czp5900 @ 2024-11-09 21:32:04

@czp5900 如果有人有建议可以加一下,方便私聊 https://www.luogu.com.cn/team/93346


|