不知道高精度怎样AC

P1009 [NOIP1998 普及组] 阶乘之和

liyifanglan @ 2024-07-13 15:07:09

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin >> n;
    long long ans = 0;
    for (int i = 1; i <= n; i++){
        long long sum = 1;
        for (int j = 1; j <= i; j++){
            sum*=j;
        }
        ans += sum;
    }
    cout << ans;
}

by Aaaa_StarRail @ 2024-07-13 15:09:45

A不了,必须用高精度 @xd23lyfl


by blsya23a2 @ 2024-07-13 15:15:32

用 python。python 不存在精度问题。


by ATION001 @ 2024-07-13 16:22:30

@xd23lyfl 看我的,直接用__int128,然后特判。

代码


#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;
    } 
    for(int i=1;i<=n;i++){
        b[i]=b[i-1]*i;
        sum+=b[i];
    }
    print(sum);
    return 0;
}

by liyifanglan @ 2024-07-13 16:42:04

谢谢!!!


by purplepolarstar @ 2024-07-13 22:07:50

试试我的吧,没有那么复杂的AC

#include<bits/stdc++.h>
#define N 500
#define LoveLive unsigned long long
const LoveLive lim=(1uLL<<60)-1;
struct istream{
    template<typename T>
        inline istream&operator>>(T&d){
            static int c;
            while(!isdigit(c=getchar()));
            for(d=0;isdigit(c);c=getchar())
                d=(d<<3)+(d<<1)+(c^'0');
            return*this;
        }
}cin;
int T_,n,m,kind[N][N];
int cc,cnt,fa[N*N];
int g[N][N];
LoveLive dis[N*N],r[N*N],dot[N*N],V[N*N];
std::pair<int,int>pr[N][N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
struct edge{
    int u,v;
    bool b;
}e[N*N];
struct random{
    LoveLive seed;
    inline void srand(LoveLive s){
        seed=s;
    }
    inline LoveLive operator()(){return(((seed*=37)+=7)*=19260817)^=31;}
}Rand;
#define rand Rand
struct Tree{
    struct EDGE{
        int to,nxt,id;
    }e[N*N<<2];
    int cnt,head[N*N<<1];
    inline void init(){
        memset(head,0,sizeof head);
        cnt=0;
    }
    inline void addedge(int u,int v,int id){
        e[++cnt]=(EDGE){v,head[u],id};
        head[u]=cnt;
        e[++cnt]=(EDGE){u,head[v],id};
        head[v]=cnt;
    }
    void dfs(int now,int pre){
        for(int i=head[now];i;i=e[i].nxt)
        if(e[i].to!=pre){
            dfs(e[i].to,now);
            dis[e[i].id]=r[e[i].to]^dot[e[i].to];
            dot[now]^=dis[e[i].id];
        }
    }
}T;
int main(){
    rand.srand(79877863023177851LL);
    for(cin>>T_;T_--;){
        cin>>n>>m;
        memset(kind,0,sizeof kind);
        memset(g,0,sizeof g);
        memset(dot,0,sizeof dot);
        memset(dis,0,sizeof dis);
        T.init();
        for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        cin>>kind[i][j];
        cc=0;
        for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j){
            pr[i][j]=std::make_pair(0,0);
            if(kind[i][j]==1||kind[i][j]==3)cin>>r[pr[i][j].first=++cc];
            if(kind[i][j]==2||kind[i][j]==3)cin>>r[pr[i][j].second=++cc];
        }
        cnt=0;
        for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        if(kind[i][j]==4){
            if(pr[i-1][j].first&&!pr[i][j].first)pr[i][j].first=pr[i-1][j].first;
            if(pr[i][j-1].second&&!pr[i][j].second)pr[i][j].second=pr[i][j-1].second;
        }
        for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        if(kind[i][j]==4){
            e[g[i][j]=++cnt]=(edge){pr[i][j].first,pr[i][j].second,0};
        }
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j)
            if(kind[i][j]==4){
                if(pr[i][j].first==0||pr[i][j].second==0){
                    e[g[i][j]].b=1;
                    dis[g[i][j]]=rand()%lim+1;
                }
                if(!pr[i][j].first&&pr[i][j].second){
                    dot[pr[i][j].second]^=dis[g[i][j]];
                }else
                if(pr[i][j].first&&!pr[i][j].second){
                    dot[pr[i][j].first]^=dis[g[i][j]];
                }
            }
        }
        for(int i=1;i<=cc;++i)fa[i]=i;
        for(int i=1;i<=cnt;++i)
        if(e[i].u&&e[i].v&&find(e[i].u)!=find(e[i].v)){
            T.addedge(e[i].u,e[i].v,i);
            fa[find(e[i].v)]=e[i].u;
            e[i].b=1;
        }
        for(int i=1;i<=cnt;++i)if(!e[i].b){
            dis[i]=rand()%lim+1;
            dot[e[i].u]^=dis[i];
            dot[e[i].v]^=dis[i];
        }
        T.dfs(1,1);
        bool ok=1;
        memset(V,0,sizeof V);
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j)
            if(kind[i][j]==4){
                if(pr[i][j].first)V[pr[i][j].first]^=dis[g[i][j]];
                if(pr[i][j].second)V[pr[i][j].second]^=dis[g[i][j]];
            }
        }
        for(int x=1;x<=cc&&ok;++x)
        if(V[x]!=r[x]){
            bool yes=0;
            for(int i=1;i<=n&&!yes;++i){
                for(int j=1;j<=m&&!yes;++j)
                if(kind[i][j]==4)
                if(pr[i][j].first==x&&!pr[i][j].second||pr[i][j].second==x&&!pr[i][j].first){
                    dis[g[i][j]]^=V[x]^r[x];
                    yes=1;
                }
            }
            ok&=yes;
        }
        static std::unordered_set<LoveLive>st;
        st.clear();
        for(int i=1;i<=cnt&&ok;++i)
        if(!dis[i]||st.count(dis[i]))ok=0;else st.insert(dis[i]);
        if(ok){
            for(int i=1;i<=n;++i){
                for(int j=1;j<=m;++j)if(g[i][j])printf("%llu ",dis[g[i][j]]);
                putchar('\n');
            }
        }else puts("-1");
    }
    return 0;
}

by purplepolarstar @ 2024-07-13 22:12:54

c++不用高精度也能解

(上一个发错代码了怎么办呜呜呜...)

重发一遍,请各位忽略上一个帖子:

试试我的吧,没有那么复杂的AC

#include<bits/stdc++.h>

using namespace std;

int main(){

    int i,A[1005]={0},B[1005]={0},n,j;

    cin>>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--) cout<<A[j];

    return 0;

}

用数列写的,看不懂请私信。


by liyifanglan @ 2024-07-13 22:14:39

额~~~(脑容量貌似有点不够)


by a0o0o @ 2024-07-19 14:29:22

@linjingyi 厉害


by LiuYongHaoLYH @ 2024-07-19 20:05:14

@linjingyi 哥差点没把我吓死


by purplepolarstar @ 2024-07-22 15:04:54

@LiuYongHaoLYH 我是女的


| 下一页