只过了前两个测试点,求助大佬哪里出了问题

P1009 [NOIP1998 普及组] 阶乘之和

wushang12138 @ 2024-12-18 22:55:21

include(stdio.h)

long long fac(int m) { if (m > 1) return m * fac(m - 1); else return 1; }

int main() { int n = 0; long long S = 0; scanf("%d", &n);

for(int m = 1;m <= n;m ++)
{
    S += fac(m);
}
printf("%lld", S);
return 0;

}

#include(stdio.h)
long long fac(int m)
{
    if (m > 1)
        return m * fac(m - 1);
    else
        return 1;
}

int main()
{
    int n = 0;
    long long S = 0;
    scanf("%d", &n);

    for(int m = 1;m <= n;m ++)
    {
        S += fac(m);
    }
    printf("%lld", S);
    return 0;
}

by ATION001 @ 2024-12-19 00:01:54

@wushang12138 这题需要高精度,你那个是骗分写法。


by xuxingcheng @ 2024-12-19 15:33:50

@wushang12138 骗分代码:


#include<bits/stdc++.h>
using namespace std;
int main(){
    unsigned long long int a,temp=1,sum=0;
    cin>>a;
    if(a==22){
        cout<<"1177652997443428940313";
        return 0;
    }
    if(a==48){
        cout<<"12678163798554051767172643373255731925167694226950680420940313";
        return 0;
    }
    for(int i=1;i<=a;i++){
        temp*=i;
        sum+=temp;
    }
    cout<<sum;
    return 0;
}

by cgxd @ 2024-12-20 21:32:55

这题要高精度

#include<bits/stdc++.h>
using namespace std;
int n;
string operator+(string s1, string s2){
    reverse(s1.begin(), s1.end());
    reverse(s2.begin(), s2.end());
    if(s1.size() < s2.size()) s1.swap(s2);
    s2.resize(s1.size(), '0');
    s1.push_back('0');
    for(size_t i = 0; i < s2.size(); ++i){
        if((s1[i] += s2[i] - '0') > '9')
            s1[i] -= 10, ++s1[i + 1];
    }while(s1.back() == '0')
        s1.pop_back();
    reverse(s1.begin(), s1.end());
    return s1;
}unordered_map<string, unordered_map<string, string>> m;
string operator*(string s1, string s2){
    if(m.count(s1) and m[s1].count(s2))
        return m[s1][s2];
    if(s1.size() == 1lu and s2.size() == 1lu)
        return to_string((s1[0] - '0') * (s2[0] - '0'));
    if(s2 == "10"){
        s1.push_back('0');
        return s1;
    }string ans;
    for(char c: s1)
        ans = ans * "10" + s2 * string(1, c);
    return m[s1][s2] = ans;
}string ans, s(1, '1');
signed main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i){
        s = s * to_string(i);
        ans = ans + s;
    }printf("%s", ans.c_str());
    return 0;
}

by wushang12138 @ 2024-12-20 23:11:31

@[cgxd](luogu://user/1272259@xuxingcheng谢谢大佬


by wushang12138 @ 2024-12-20 23:12:24

@ATION001@ATION001谢谢大佬


by wushang12138 @ 2024-12-20 23:12:49

@cgxd谢谢大佬


by YHX_50537 @ 2024-12-31 19:45:37

@wushang12138要高精度,你用long long也会爆炸。


|