麦基数(p1045)

描述:
\(计算2^{P}−1的位数和最后500位数字(用十进制高精度数表示)\)

Ⅰ.求位数

\(因为2^p最后一位必定不为0,求2^p-1的位数也就是求2^p位数\)
\(2^p的位数确实很难求,但10^p我们是知道的,位数等于p+1\)
\(为此我们可以采用换底公式,2=10的log10(2)次方\)
\(所以答案就是求(int)(log10(2)*p+1))\)

接下来就是高精度快速幂了,只需要用每次的最后500位做乘法就好了

#include <bits/stdc++.h>
using namespace std;
int p,a[1009],b[1009],temp[1009];
void ji1()
{
	memset(temp,0,sizeof(temp));
	for(int i=1;i<=500;i++)
	for(int j=1;j<=500;j++)
	{
		temp[i+j-1]+=a[i]*b[j];
		temp[i+j]+=temp[i+j-1]/10;
		temp[i+j-1]%=10;
	}
	memcpy(a,temp,sizeof(temp));
}
void ji2()
{
	memset(temp,0,sizeof(temp));
	for(int i=1;i<=500;i++)
	for(int j=1;j<=500;j++)
	{
		temp[i+j-1]+=b[i]*b[j];
		temp[i+j]+=temp[i+j-1]/10;
		temp[i+j-1]%=10;
	}
	memcpy(b,temp,sizeof(temp));
}
int main()
{
	cin>>p;
	cout<<int(log10(2)*p+1)<<endl;
	a[1]=1;b[1]=2;
	while(p)
	{
		if(p&1)	ji1();
		p>>=1;
		ji2();
	}
	a[1]-=1;
	for(int i=500;i>=1;i--)
	{
		if(i%50==0&&i!=500)	cout<<endl<<a[i];
		else	cout<<a[i];
	}
	return 0;
}
posted @ 2020-04-02 16:30  倾叶子佮  阅读(123)  评论(0编辑  收藏  举报