超时且全错,求助!!!

P1045 [NOIP2003 普及组] 麦森数

mmdxm @ 2022-08-15 12:40:44

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int z[1000101],len=1,y[1000101];
void quick(int l){
    if(l==1) return;
    int k=l>>1,x=0,i,j;
    quick(k);
    for(i=1;i<=len;i++){
        for(j=1;j<=len;j++){
            y[i+j-1]+=z[i]*z[j];
            y[i+j]+=y[i+j-1]/10;
            if(i+j>len&&y[i+j]!=0) len=i+j;
            y[i+j-1]%=10;
        }
    }
    for(i=1;i<=len;i++){
        z[i]=y[i];
        y[i]=0;
    }
    if(l&1){
        for(i=1;i<=len;i++){
        z[i]=z[i]*2+x;
        x=z[i]/10;
        z[i]%=10;
        }
        if(x!=0){
            len++;
            z[len]=x;
        }
    }
}
int main(){
    int n,i,j,x=0,k;
    scanf("%d",&n);
    z[1]=2;
    quick(n);
    z[1]-=1;
    for(i=1;i<=len;i++){
        if(z[i]<0){
            z[i]+=10;
            z[i+1]--;
        }
        else break;
    }
    printf("%d\n",len);
    for(i=500;i>=1;i--){
        printf("%d",z[i]);
        if(i%50==0&&i!=500) printf("\n");
    }
    return 0;
} 

by dengyujie2020 @ 2022-08-15 13:12:11

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int z[1000101],len=1,y[1000101];
void quick(int l){
    if(l==1) return;
    int k=l>>1,x=0,i,j;
    quick(k);
    for(i=1;i<=len;i++){
        for(j=1;j<=len;j++){
            y[i+j-1]+=z[i]*z[j];
            y[i+j]+=y[i+j-1]/10;
            if(i+j>len&&y[i+j]!=0) len=i+j;
            y[i+j-1]%=10;
        }
    }
    for(i=1;i<=len;i++){
        z[i]=y[i];
        y[i]=0;
    }
    if(l&1){
        for(i=1;i<=len;i++){
        z[i]=z[i]*2+x;
        x=z[i]/10;
        z[i]%=10;
        }
        if(x!=0){
            len++;
            z[len]=x;
        }
    }
}
int main(){
    int n,i,j,x=0,k;
    scanf("%d",&n);
    z[1]=2;
    quick(n);
    z[1]-=1;
    printf("%d\n",len);
    for(i=500;i>=1;i--){
        if(i%50==0&&i!=500) printf("\n");
        printf("%d",z[i]);

    }
    return 0;
} 

by dengyujie2020 @ 2022-08-15 13:12:42

超时是算法问题,WA是换行问题


by Chtholly_Tree @ 2022-08-15 13:23:55

#include<bits/stdc++.h>
using namespace std;
int p, rev[1100], f[1100], sav[1100];

void cheng1(){//rev * f
    memset(sav, 0, sizeof(sav));
    for(int i = 1; i <= 500; i++){
        for(int j = 1; j <= 500; j++){
            sav[i + j - 1] += rev[i] * f[j];
            sav[i + j] += sav[i + j - 1] / 10;
            sav[i + j - 1] %= 10;
        }
    }
    memcpy(rev, sav, sizeof(rev));
}

void cheng2(){//f * f
    memset(sav, 0, sizeof(sav));
    for(int i = 1; i <= 500; i++){
        for(int j = 1; j <= 500; j++){
            sav[i + j - 1] += f[i] * f[j];
            sav[i + j] += sav[i + j - 1] / 10;
            sav[i + j - 1] %= 10;
        }
    }
    memcpy(f, sav, sizeof(f));
}

int main(){
    cin >> p;
    cout << (int)(p*log10(2) + 1);
    rev[1] = 1;
    f[1] = 2;
    while(p != 0){
        if(p % 2 == 1)
            cheng1();
        p /= 2;
        cheng2();
    }
    rev[1]--;
    for(int i = 500; i >= 1; i--){
        if(i % 50 == 0) cout << "\n";
        cout << rev[i];
    }

    return 0;
}

by Chtholly_Tree @ 2022-08-15 13:24:05

@liuyufei0925


by mmdxm @ 2022-08-15 14:03:28

我的输出为什么不对呢?@Xuzhonghan666


by Chtholly_Tree @ 2022-08-15 14:10:49

@liuyufei0925 减 1 !!!


by mmdxm @ 2022-08-15 14:10:53

哦,没逝了,我眼实在太瞎了


by mmdxm @ 2022-08-15 14:12:23

不是减一的问题吧?(我又瞎了)


by mmdxm @ 2022-08-15 14:14:20

 z[1]-=1;
    for(i=1;i<=len;i++){
        if(z[i]<0){
            z[i]+=10;
            z[i+1]--;
        }
        else break;
    }

这里就是在减一啊


by Chtholly_Tree @ 2022-08-18 10:32:59

@liuyufei0925 那我瞎了我再帮你看看


| 下一页