洛谷P1045 [NOIP2003 普及组] 麦森数

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
unsigned long long n,result[1000000] = {0,1},result2[1000000];
int main()
{
	freopen("title.in", "r", stdin);
	cin >> n;
	unsigned long long int nodigits = ceil(n /3.3219280948873), temp = pow(2, 50);//利用10^log10(2)=2计算位数,temp存储2的50次方,方便后面一次乘50次
	cout << nodigits << endl;
	nodigits = 1;//减少循环
	for (int i = 1; i <= n / 50; i++)
	{
		for (int t = 1; t <= nodigits; t++)
		{
			result[t] *= temp;
		}
		for (int t = 1; t <= nodigits; t++)
		{
			if (result[t] > 9)//进位,主要是这里费时间
			{
				result[t + 1] += result[t] / 10;
				result[t] = result[t] % 10;
				if (nodigits < 500 && t == nodigits)
					nodigits++;
			}
			else
				break;
		}
	}
	
	for (int i = 1; i <= n % 50; i++)//要将不满50的乘进去
	{
		for (int t = 1; t <= nodigits; t++)
		{
			result[t] *= 2;
		}
	}
	for (int t = 1; t <= nodigits; t++)//一定不超过2的50次方,故将进位放在循环外减少循环次数
	{
		if (result[t] > 9)
		{
			result[t + 1] += result[t] / 10;
			result[t] = result[t] % 10;
			if (nodigits < 500 && t == nodigits)
				nodigits++;
		}
	}
	int t = 1;
	while (1)//减1操作
	{
		if (result[t] > 1)
		{
			result[t] -= 1;
			break;
		}
		else if (result[t] == 0)
		{
			result[t] = 9;
			t++;
		}
	}
	for (int i = 500; i > 0; i--)
	{
		cout << result[i];
		if (!((i - 1) % 50))//防止500时直接换行
			cout << endl;
	}
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值