TLE四个点求助

P1045 [NOIP2003 普及组] 麦森数

zhuzhuziziyangyang @ 2023-05-13 14:46:40

#include<bits/stdc++.h>
using namespace std;
const int N=10000001;
int a[N];
int main(){
    int n;
    cin>>n;
    a[1]=1;
    int l,x;
    for(int i=1;i<=n;i++){
        x=0;
        for(int j=1;j<=500;j++){
            a[j]=a[j]*2+x;
            x=0;
            if(a[j]>=10){
                x=a[j]/10;
                a[j]%=10;
            }
        }
    }
    l=ceil(log10(2)*n); 
    cout<<l<<endl;
    a[1]--;
    for(int i=500;i>=1;i--){
        cout<<a[i];
        if((i-1)%50==0){
            cout<<endl;
        }
    }
    return 0;   
}

by bsdsdb @ 2023-05-13 15:11:00

要用快速幂 虽然我全RE


by zhuzhuziziyangyang @ 2023-05-15 12:25:37

@FeiWuLiuZiao 谢谢大佬


by Life_passing_by @ 2023-07-25 09:26:29

@zhuzhuziziyangyang 高精压位,一次计算60次幂,然后进位,这样不用快速幂

#include <bits/stdc++.h>
using namespace std;
unsigned long long a[510];
long long N;
int main()
{
    cin >> N;
    cout<<(long long)(log10(2)*N+1)<<endl;
    a[500]=2;
    N--; 
    for(;N>0;N-=60){
        for(int i=500;i>0;i--)(N>60?(a[i]<<=60):(a[i]<<=N));
        for(int i=500;i>0;i--)if(a[i]>=10)a[i-1]+=a[i]/10,a[i]%=10;
    }
    a[500]--;
    for(int i=0;i<=9;i++){
        for(int j=1;j<=50;j++)cout<<a[i*50+j];
        cout<<endl;
    }
    return 0;
}

by zhuzhuziziyangyang @ 2023-09-01 12:18:25

@Life_passing_by 谢谢大哥


|