Rakutani P1045 wheat forest number (high-precision, place the number of official)

Original title link: https: //www.luogu.com.cn/problem/P1045

Seeking 2 P last 500 bits (P <3100000) -1 of the

Previous encounter with high precision is python Dafa is good
to write about the c high precision, for the first time to write, stepped on a lot of pit
this problem is a high-precision rapid multiplication + power

Q is seeking the first small bits can be used to find the median of a formula
K = log10 (N) + 1'd
2 P -1 and 2 P bits are the same, so a direct seek 2 P bits to the
log10 (2 P ) = P * loglO (2)

Code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[1010],s[1010],t[1010];
void result1()
{
    memset(t,0,sizeof(t));
    for(int i=1;i<=500;i++)
    {
        for(int j=1;j<=500;j++)
        {
            t[i+j-1]+=s[i]*a[j];
        }
    }
    for(int i=1;i<=500;i++)
    {
        t[i+1]+=t[i]/10;
        t[i]%=10;
        s[i]=t[i];
    }
}
void result2()
{
    memset(t,0,sizeof(t));
    for(int i=1;i<=500;i++)
    {
        for(int j=1;j<=500;j++)
        {
            t[i+j-1]+=a[j]*a[i];
        }
    }
    for(int i=1;i<=500;i++)
    {
        t[i+1]+=t[i]/10;
        t[i]%=10;
        a[i]=t[i];
    }
}
int main()
{
    ll p;
    cin>>p;
    ll kk=p*log10(2)+1;
    s[1]=1;a[1]=2;
    while(p)
    {
        if(p&1)
        {result1();}
        result2();
        p/=2;
    }
    s[1]--;
    cout<<kk<<endl;
    for(int i=1;i<=500;i++)
    {
        int c=500-i+1;
        if(i%50==0)
        {cout<<s[c]<<endl;}
        else
        {cout<<s[c];}
    }
    return 0;
}

Published 36 original articles · won praise 4 · Views 1391

Guess you like

Origin blog.csdn.net/qq_43781431/article/details/104562860