WA+TLE,求助

P1045 [NOIP2003 普及组] 麦森数

shengzhaoteng1118 @ 2024-12-11 10:14:16

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,la,a[1000001],f=1;
void cheng(){
    for(int i=1;i<=la;i++){
        a[i]*=2;
    }
    for(int i=1;i<=la;i++){
        if(a[i]>9){
            a[i+1]++;
            a[i]-=10; 
        }
    }
    if(a[la+1]>0){
        la++;
    }
    return ;
}
signed main(){
    cin>>n;
    a[1]=1;
    la=1;
    for(int i=1;i<=n;i++){
        cheng();
    }
    a[1]-=1;
    cout<<la<<endl;
    for(int i=500;i>=1;i--){
        cout<<a[i];
    }
    return 0;
}

by wangqiyu_why @ 2024-12-15 15:42:13

@shengzhaoteng1118 Wa的原因是没考虑换行,但如果想拿全分,建议学一下快速幂


#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,la,a[1000001],f=1;
void cheng(){
    for(int i=1;i<=la;i++){
        a[i]*=2;
    }
    for(int i=1;i<=la;i++){
        if(a[i]>9){
            a[i+1]++;
            a[i]-=10; 
        }
    }
    if(a[la+1]>0){
        la++;
    }
    return ;
}
signed main(){
    cin>>n;
    a[1]=1;
    la=1;
    for(int i=1;i<=n;i++){
        cheng();
    }
    a[1]-=1;
    cout<<la<<endl;
    for(int i=500;i>=1;i--){
        cout<<a[i];
        if((i-1)%50==0)
        {
            cout<<endl;
        }
    }
    return 0;
}

by shengzhaoteng1118 @ 2024-12-16 10:01:12

@wangqiyu_why Thank you very much!


|