菜鸡求助

P1045 [NOIP2003 普及组] 麦森数

Lovable_Wind @ 2021-06-14 20:55:49

#include<bits/stdc++.h>
using namespace std;
const double pi=3.14;
const int inf=0x3f3f3f3f;
const int NIL=-1;
#define f(i,l,r) for(int i=l;i<=r;i++)
int m[100010],n=1;
void times(int a){
    int tmp=0;
    for (int i=1;i<=500;i++){
        int tmp=(m[i]*a+tmp)/10;
        m[i]=(m[i]*a+tmp)%10;
    }
    if (tmp!=0){
        while(tmp>0&&n<=500){
            m[++n]=tmp%10;
            tmp/=10;
        }
        tmp=0;
    }

}
int read()
{
    int ans=0,flag=1;
    char ch=getchar();
    while( (ch>'9' || ch<'0') && ch!='-' ) ch=getchar();
    if(ch=='-') flag=-1,ch=getchar();
    while(ch>='0' && ch<='9') ans=ans*10+ch-'0',ch=getchar();
    return ans*flag;
}
int main()
{
    int p;
    p=read();
    m[1]=1;
    cout<<int(log10(2.0)*p+1)<<endl;
    for (int i=1;i<=p;i++){
        times(2);
    }
    int dk=50;
    for (int i=500;i>=1;i--){
        if (dk==1){
            dk=50;
            cout<<endl;
        }
        cout<<m[i];
        dk--;
    }
}

样例给进去之后输出的位数和结果都不对


by delic1ous @ 2021-08-10 20:29:53

1.times函数里,你定义了两个tmp;循环里的赋值有问题;if(tmp!=0)语句块可去掉

2.主函数输出时判断有问题,应该改为if(dk==0)

3.m[1]忘记减一了

#include<bits/stdc++.h>
using namespace std;
const double pi=3.14;
const int inf=0x3f3f3f3f;
const int NIL=-1;
#define f(i,l,r) for(int i=l;i<=r;i++)
int m[100010],n=1;
void times(int a){
    int tmp=0;
    for (int i=1;i<=500;i++){
        tmp+=m[i]*a;
        m[i]=tmp%10;
        tmp/=10;
   }
}
int read()
{
    int ans=0,flag=1;
    char ch=getchar();
    while( (ch>'9' || ch<'0') && ch!='-' ) ch=getchar();
    if(ch=='-') flag=-1,ch=getchar();
    while(ch>='0' && ch<='9') ans=ans*10+ch-'0',ch=getchar();
    return ans*flag;
}
int main()
{
    int p;
    p=read();
    m[1]=1;
    cout<<int(log10(2.0)*p+1)<<endl;
    for (int i=1;i<=p;i++){
        times(2);
    }
    m[1]-=1;
    int dk=50;
    for (int i=500;i>=1;i--){
        if (dk==0){
            dk=50;
            cout<<endl;
        }
        cout<<m[i];
        dk--;
    }
}

修改后可以过样例和部分数据,但复杂度太高(300w*500)会出现TLE,建议每次乘2^25(结合压位更快)可过,最优解为快速幂加压位


|