使用对数lg直接估算所求位数, 每次乘以2^60大大加快速度(不够60再乘以更小的)
9*2^60为10,376,293,541,461,622,784刚好不会超ull范围(18,446,744,073,709,551,616)
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef unsigned long long ull;
ull a[501]={1}; //初始化为1
int main()
{
int p;
cin >> p;
cout << (int)(p*log10(2)) + 1 << endl;//直接用lg估算位数
for(;p > 0;p -= 60)//每次减掉60次幂
{
ull f = 0;//进位
for(int i = 0;i < 500;i ++)
{
if(p > 60) a[i] <<= 60; //一次性乘2^60, 9*2^60为10,376,293,541,461,622,784刚好不会超ull范围(18,446,744,073,709,551,616)
else a[i] <<= p;//如果剩下的不够60了就不要乘60了,乘p
a[i] += f;
f = a[i] / 10;
a[i] %= 10;
}
}
a[0]-=1;
for(int i = 499;i >= 0;i --)
{
printf("%d", a[i]);
if(i % 50 == 0) puts("");
}
return 0;
}
标签:麦森数,int,long,60,ull,P1045,include
From: https://www.cnblogs.com/acing/p/18550840