題目描述
形如2P−12{P}-12P−1的素數稱爲麥森數,這時PPP一定也是個素數。但反過來不一定,即如果PPP是個素數,2P−12{P}-12P−1不一定也是素數。到1998年底,人們已找到了37個麥森數。最大的一個是P=3021377P=3021377P=3021377,它有909526位。麥森數有許多重要應用,它與完全數密切相關。
任務:從文件中輸入PPP(1000<P<31000001000<P<31000001000<P<3100000),計算2P−12^{P}-12P−1的位數和最後500位數字(用十進制高精度數表示)
輸入格式
文件中只包含一個整數PPP(1000<P<31000001000<P<31000001000<P<3100000)
輸出格式
第一行:十進制高精度數2P−12^{P}-12P−1的位數。
第2-11行:十進制高精度數2P−12^{P}-12P−1的最後500位數字。(每行輸出50位,共輸出10行,不足500位時高位補0)
不必驗證2P−12^{P}-12P−1與PPP是否爲素數。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
int a[600];
int ans[1200];
int cur[1200];
void solve1()
{
mem(cur, 0);
for(int i = 1; i <= 500; i++)
for(int j = 1; j <= 500; j++)
{
cur[i + j - 1] += ans[i] * a[j];
cur[i + j] += cur[i + j - 1] / 10;
cur[i + j - 1] %= 10;
}
// cout << cur[1] << cur[2] << endl;
memcpy(ans, cur, sizeof(ans));
}
void solve2()
{
mem(cur, 0);
for(int i = 1; i <= 500; i++)
for(int j = 1; j <= 500; j++)
{
cur[i + j - 1] += a[i] * a[j];
cur[i + j] += cur[i + j - 1] / 10;
cur[i + j - 1] %= 10;
}
// cout << a[2] << a[1] << ' ';
memcpy(a, cur, sizeof(a));
}
int main()
{
int p;
cin >> p;
cout << int(log10(2) * p) + 1 << endl;
ans[1] = 1;
a[1] = 2;
while(p != 0)
{
// cout << ans[1] << ans[2] << endl;
if(p & 1)
{
solve1();
}
p >>= 1;
solve2();
// cout << a[3] << a[2] << a[1] << endl;
}
ans[1] -= 1;
for(int i = 500; i > 0; i--)
{
if(i != 500 && i % 50 == 0)
cout << endl;
cout << ans[i];
}
return 0;
}