麦森数超时谁可以帮我找个测长度的办法

P1045 [NOIP2003 普及组] 麦森数

伯爵H @ 2017-07-03 10:34:21

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
int a[10000001];
bool s[10000001];
int p,x=0;
int cd();
int main() {
    //freopen("mason.in","r",stdin);
    //freopen("mason.out","w",stdout);
    cin>>p;
    a[1]=1;
    memset(s,0,sizeof(s));
    s[1]=1;
    long long j;
    for(int  i=1; i<=p; i++) {
        for( j=1; j<=cd(); j++) {
            a[j]*=2;
            a[j]+=x;
            x=a[j]/10;
            if(x!=0) {
                s[j+1]=1;
            }
            a[j]%=10;
        }
    }
    int k=0;
    cout<<cd()<<endl;
    for(int i=500; i>=2; i--) {
        cout<<a[i];
        k++;
        if(k==50) {
            cout<<endl;
            k=0;
        }
    }
    cout<<a[1]-1;
    return 0;
}
int cd() {
    int f=1;
    for(int i=1; i<=sqrt(1000000); i++) {
        if(s[i]==0) {
            return i-1;
        }
    }
}

by qianfujia @ 2017-07-25 15:29:01

trunc(ln(2)/ln(10)*n)+1


by qianfujia @ 2017-07-25 15:29:34

@ Randywolf


by LMB_001 @ 2017-08-25 13:41:22

布吉岛诶


|