**50求助**

P1009 [NOIP1998 普及组] 阶乘之和

HuangSiHan3116 @ 2024-07-18 22:26:34

50求助

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

by HuangSiHan3116 @ 2024-07-18 22:28:11

不知道高精度


by jinhaoxuan001 @ 2024-07-19 13:06:11

@Hsh663116 高精度的意思就是比long long上限还大的数


by superboy3 @ 2024-07-20 19:14:10

没用高精度


by xuchuze @ 2024-07-21 15:13:37

#include<bits/stdc++.h>
using namespace std;
int n;
vector<int>A,s;
vector<int>add(vector<int> A,vector<int>B)
{
    int t=0;
    vector<int>C;
    for(int i=0;i<A.size()||i<B.size();i++)
    {
        if(i<A.size()) t+=A[i];
        if(i<B.size()) t+=B[i];
        C.push_back(t%10);
        t/=10;
    }
    if(t) C.push_back(1);
    return C;
}
vector<int> sub(vector<int> A,int b)
{
    int t=0; 
    vector<int>C;
    for(int i=0;i<A.size()||t;i++)
    {
        if(i<A.size()) t+=A[i]*b;
        C.push_back(t%10);
        t/=10;
    }
    return C;
}
int main(void)
{
    cin>>n;
    A.push_back(1),s.push_back(0);
    for(int i=1;i<=n;i++)
    {
        A=sub(A,i);
        s=add(s,A);
    }
    for(int i=s.size()-1;i>=0;i--) cout<<s[i];
    return 0;
}

by amd47802574 @ 2024-07-21 17:11:05

不知道高精度就去搜一下相关视频,这题要用到加法和乘法板子

string Multiply(string x,string y)
{
    int a[10000] = { 0 }, b[10000] = { 0 }, c[10000] = { 0 };
    int la, lb, lc;
    la = x.length();
    lb = y.length();
    //字符串转数组
    for (int i = 0; i < max(la, lb); i++) {
        if (i < la) {
            a[la - i] = x[i] - '0';
        }
        if (i < lb) {
            b[lb - i] = y[i] - '0';
        }
    }
    //c[]数组存储运算结果
    for (int i = 1; i <= la; i++) {
        for (int j = 1; j <= lb; j++) {
            c[i + j - 1] += a[i] * b[j];
            c[i + j] += c[i + j - 1] / 10;
            c[i + j - 1] %= 10;
        }
    }
    //计算c[]数组长度
    lc = la + lb;
    //删除前导0,至少保留1位
    while (c[lc] == 0 && lc > 1) lc--;
    string k;
    for (int i = lc; i > 0; i--) k += to_string(c[i]);
    return k;
}

string Add(string x,string y)
{
    int a[10000] = { 0 }, b[10000] = { 0 }, c[10000] = {0};
    int la, lb, lc;
    la = x.length();//第一个数的长度
    lb = y.length();//第二个数的长度
    //将第一个字符串转为数字数组
    for (int i = 0; i < la; i++) {
        a[la - i] = x[i] - '0';
    }
    //将第二个字符串转为数字数组
    for (int i = 0; i < lb; i++) {
        b[lb - i] = y[i] - '0';
    }
    lc = max(la, lb);
    //取出长度最大值
    //运算,用c数组来存储
    for (int n = 1; n <= lc; n++) {
        c[n] += a[n] + b[n];
        c[n + 1] = c[n] / 10;
        c[n] = c[n] % 10;
    }
    //当两个数相加进位超过2个数各自的位数
    if (c[lc + 1] > 0) lc++;
    string z;
    for (int i = lc; i > 0; i--) z += to_string(c[i]);
    return z;
}

|