p1142 Number Theory? + high precision + fast power

  When I came up, I saw 500 digits. Well, the high precision is undoubtedly.

  Then think about the first question:      

    Through http://wenku.baidu.com/view/b2ca872014791711cd79170b.html I learned that the digit of a large number is the sum of its factor digits. Although it is a little different from this problem, the factors can also be divided into p 2. Then the number of digits becomes int(p*log10(double(2))+1). First question solved.

  Then the second question:

    It's not too difficult, just multiply by 2 each time. I still know how to multiply one digit with high precision. I wrote it with confidence, and of course there will be big data to pass after I hand it in. Calculate the time complexity, embarrassing. . .

    Then I naturally thought of fast power. Because I was very impressed with this thing during the provincial election. Then about a=a*b. How to get b=b*b?

    As a large number of 500 digits, if it is still directly overwritten, it will not work, it will affect the next operation. On this issue, I thought about it for two days, two days!

    Then I thought that I could temporarily use an array to store large numbers, and I felt sorry for my IQ. So this question is completed.

using  namespace std;
 int i,f, p,t ,lena,lenb;
 int a[ 2000 ],b[ 2000 ],ta[ 2000 ],tb[ 2000 ]; // It is impossible to use a structure, as long as it is not Need to sort 
void aab() // a=a*b; 
{
     for (i= 1 ;i<=lena;i++ )
    {
        ta [i] = a [i];
        a[i]=0;
    }
    t=lena;
    for(i=1;i<=lena;i++)
        for(f=1;f<=lenb;f++)
        {
            int k=i+f-1;
            a[k]+=ta[i]*b[f];
            while(a[k]>=10)
            {
                a[k+1]+=a[k]/10;
                a[k]%=10;
                k++;
            }
            if(k>t)t=k;
        }
    lena=min(t,510);
    while((a[lena]==0)&&(lena>1))    lena--;
}
void bbb()
{
    for(i=1;i<=lenb;i++)
    {
        tb[i]=b[i];
        b[i]=0;
    }
    t=lenb;
    for(i=1;i<=lenb;i++)
        for(f=1;f<=lenb;f++)
        {
            
            int k=i+f-1;            
            b[k]+=tb[i]*tb[f];
            while(b[k]>=10)
            {
                b[k+1]+=b[k]/10;
                b[k]%=10;
                k++;
            }
            if(k>t)t=k;
        }
    lenb=min(t,510);
    while((b[lenb]==0)&&(lenb>1))lenb--;

}
intmain ()
{ios::sync_with_stdio(false);
    cin>>p;
    cout<<int(p*log10(2.0)+1)<<endl;
//////////////////////////////////////////
    lena=1;
    lenb=1;
    a[1]=1;
    b[1]=2;
    while(p!=0)
    {
        
        if(p&1)
        {
            aab (); // a = a*b; 
        }
        p>>=1;
        bbb();//b=b*b;
    }
    for(i=500;i>1;i--)cout<<a[i];
    cout<<a[i]-1;
}
p1142

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325075483&siteId=291194637