怎么高精度啊!!!!!!!!!

P1009 [NOIP1998 普及组] 阶乘之和

Oscar111111 @ 2024-07-02 13:20:05

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int n,S=0,a;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        a=1;
        for(int j=1;j<=i;j++)
        {
           a=a*j;
        }
        S=S+a;
    }
    cout<<S;
    return 0;
}

by OIjoker @ 2024-07-02 13:27:10

建议

建议去ACWING,

学习算法基础课。

或者自己买书看,

貌似是板子题喵。


by Oscar111111 @ 2024-07-02 13:29:33

YES


by King_and_Grey @ 2024-07-02 13:31:53

@Oscar111111

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000;
struct Bigint {
    int len, a[maxn];
    Bigint(int x = 0) {
        memset(a, 0, sizeof(a));
        for (len = 1; x; len++)
            a[len] = x % 10, x /= 10;
        len--;
    }
    int &operator[](int i) {
        return a[i];
    }
    void flatten(int L) {
        len = L;
        for (int i = 1; i <= len; i++)
            a[i + 1] += a[i] / 10, a[i] %= 10;
        for (; !a[len]; )
            len--;
    }
    void print() {
        for (int i = max(len, 1); i >= 1; i--)
            printf("%d", a[i]);
    }
};
Bigint operator+(Bigint a, Bigint b) {//重载加号
    Bigint c;
    int len = max(a.len, b.len);
    for (int i = 1; i <= len; i++)
        c[i] += a[i] + b[i];
    c.flatten(len + 1);
    return c;
}
Bigint operator*(Bigint a, int b) {//重载乘号
    Bigint c;
    int len = a.len;
    for (int i = 1; i <= len; i++)
        c[i] = a[i] * b;
    c.flatten(len + 11);
    return c;
}

int main() {
    Bigint ans(0),fac(1);
    int m;
    cin >> m;
    for(int i = 1; i <= m; i++) {
        fac = fac * i;
        ans = ans + fac;
    }
    ans.print();
    return 0;
}

给你代码


by pig1121 @ 2024-07-02 13:32:09

@Oscar111111 用python


by King_and_Grey @ 2024-07-02 13:33:06

@pig1121 对哦!python自带高精度


by pig1121 @ 2024-07-02 13:56:01

话说高精不是这么写的吗(逃

#include<iostream>
#include<fstream>
#include<cmath>
#include<vector>
#define int long long
#define endl '\n'

using namespace std;

const double pi=acos(-1.0);

struct complex{
    double r,i;
    complex(double x=0,double y=0){
        r=x;
        i=y;
    }
    complex operator+(complex b){
        return complex(r+b.r,i+b.i);
    }
    complex operator-(complex b){
        return complex(r-b.r,i-b.i);
    }
    complex operator*(complex b){
        return complex(r*b.r-i*b.i,r*b.i+i*b.r);
    }
    double abs(){
        return sqrt(r*r+i*i);
    }
};

class poly{
    public:
        vector<complex>f;
    public:
        poly(int len){
            f.resize(len+1);
        }
        poly(int len,int*a){
            f.resize(len+1);
            for(int i=0;i<=len;i++)f[i]=a[i];
        }
        poly(string s){
            f.resize(s.size());
            for(int i=s.size()-1;i>=0;i--)f[s.size()-i-1]=s[i]-'0';
        }
        void resize(int len){
            f.resize(len+1);
        }
        int size(){
            return f.size()-1;
        }
        complex val(int p){
            if(p>=f.size())return complex(0,0);
            return f[p];
        }
        void suit(){
            int pos=size();
            while(f[pos].abs()<1e-7&&pos>0)pos--;
            resize(pos);
        }
        complex&operator[](int p){
            if(p>=f.size())f.resize(p+1);
            return f[p];
        }
        void operator=(poly y){
            y.suit();
            resize(y.size());
            for(int i=0;i<=y.size();i++)f[i]=y.val(i);
        }
    protected:
        static vector<int>r;
        static int set(int siz){
            int l=0,len=1;
            while(len<=siz)len<<=1,l++;
            r.resize(len+1);
            for(int i=0;i<len;i++)r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
            return len;
        }
        void fft(int len,int type){
            resize(len);
            for(int i=0;i<len;i++)if(i<r[i])swap(f[i],f[r[i]]);
            for(int mid=1;mid<len;mid<<=1){
                complex wn(cos(pi/mid),type*sin(pi/mid));
                int step=mid<<1;
                for(int j=0;j<len;j+=step){
                    complex w(1,0);
                    for(int k=0;k<mid;k++,w=w*wn){
                        complex x=f[j+k],y=w*f[j+mid+k];
                        f[j+k]=x+y;
                        f[j+mid+k]=x-y;
                    }
                }
            }
            if(type==-1)for(int i=0;i<=len;i++)f[i].r/=len,f[i].i/=len;
        }
    public:
        friend poly operator+(poly x,poly y){
            x.suit();y.suit();
            int l=max(x.size(),y.size());
            poly ans(l);
            for(int i=0;i<=l;i++)ans[i]=x.val(i)+y.val(i);
            for(int i=1;i<=l+1;i++)ans[i].r=(int)ans[i-1].r/10,ans[i-1].r=(int)ans[i-1].r%10;
            ans.suit();
            return ans;
        }
        friend poly operator-(poly x,poly y){
            x.suit();y.suit();
            int l=max(x.size(),y.size());
            poly ans(l);
            for(int i=0;i<=l;i++)ans[i]=x.val(i)-y.val(i);
            ans.suit();
            return ans;
        }
        friend poly operator*(poly x,poly y){
            int len=set(x.size()+y.size());
            poly ans(len);
            for(int i=0;i<=ans.size();i++)ans[i].r=(int)(x.val(i).r+0.5),ans[i].i=(int)(y.val(i).r+0.5);
            ans.fft(len,1);
            for(int i=0;i<=ans.size();i++)ans[i]=ans.val(i)*ans.val(i);
            ans.fft(len,-1);
            ans.suit();
            for(int i=0;i<=ans.size();i++)ans[i].r=(int)(ans.val(i).i/2+0.5),ans[i].i=0;
            for(int i=1;i<=ans.size();i++)ans[i].r+=(int)(ans[i-1].r)/10,ans[i-1].r=(int)(ans[i-1].r)%10;
            return ans;
        }
};
vector<int> poly::r;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string x,y;
    cin>>x>>y;
    poly f(x),g(y);
    f=f*g;
    for(int i=f.size();i>=0;i--)cout<<(int)f.val(i).r;
    return 0;
}

by panrong @ 2024-07-02 16:28:38

@pig1121 TLQTJ?(无恶意)


by liyanrui0013 @ 2024-07-03 21:41:55

n=int(input())
c=0
while n>0:
    a=n
    b=1
    for i in range(1,a+1):
        b=i*b
    c+=b
    n=n-1
print(c)

这样可以秒了


by liyanrui0013 @ 2024-07-03 21:43:21

@greyandking

n=int(input())
c=0
while n>0:
    a=n
    b=1
    for i in range(1,a+1):
        b=i*b
    c+=b
    n=n-1
print(c)

这样可以不?


by Oscar111111 @ 2024-07-09 16:40:56

but....

#include<bits/stdc++.h>
int main()
{
    int i,A[1005]={0},B[1005]={0},n,j;
    scanf("%d", &n);
    A[0]=B[0]=1;
    for (i=2;i<=n;i++){
        for (j=0;j<100;j++)
            B[j]*=i;
        for (j=0;j<100;j++)
            if (B[j]>9){
                B[j+1] += B[j]/10;
                B[j]%=10;
            }
        for (j=0;j<100;j++){
            A[j]+=B[j];
            if (A[j]>9) {
                A[j+1] += A[j]/10;
                A[j]%=10;
            }
        }
    }
    for (i=100;i>=0&&A[i]==0;i--);
    for (j=i;j>=0;j--) printf("%d", A[j]);
    return 0;
}

已经AC了


|