题目描述
形如2P−12P−1的素数称为麦森数,这时PP一定也是个素数。但反过来不一定,即如果PP是个素数,2P−12P−1不一定也是素数。到1998年底,人们已找到了37个麦森数。最大的一个是P=3021377P=3021377,它有909526位。麦森数有许多重要应用,它与完全数密切相关。
任务:从文件中输入PP(1000<P<31000001000<P<3100000),计算2P−12P−1的位数和最后500位数字(用十进制高精度数表示)
输入格式
文件中只包含一个整数PP(1000<P<31000001000<P<3100000)
输出格式
第一行:十进制高精度数2P−12P−1的位数。
第2-11行:十进制高精度数2P−12P−1的最后500位数字。(每行输出50位,共输出10行,不足500位时高位补0)
不必验证2P−12P−1与PP是否为素数。
输入输出样例
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dd;
int r[1000],a[1000],c[1000],temp[1000],n;
void multiplication_1(){
memset(temp, 0, sizeof(temp));
for(int i=1;i<=500;i++)
for(int j=1;j<=500;j++)
temp[i+j-1]+=r[i]*c[j];
for(int i=1;i<=500;i++){
temp[i+1]+=temp[i]/10;
temp[i]%=10;
}
memcpy(r, temp, sizeof(r));
}
void multiplication_2(){
memset(temp, 0, sizeof(temp));
for(int i=1;i<=500;i++)
for(int j=1;j<=500;j++)
temp[i+j-1]+=c[i]*c[j];
for(int i=1;i<=500;i++){
temp[i+1]+=temp[i]/10;
temp[i]%=10;
}
memcpy(c, temp, sizeof(c));
}
int main() {
scanf("%d",&n);
printf("%d\n",(int)(log10(2)*n+1));
r[1]=1;
c[1]=2;
while (n>0) {
if(n%2==1)multiplication_1();
multiplication_2();
n/=2;
}
r[1]-=1;
for(int i=500;i>=1;i--)
if(i!=500&&i%50==0)printf("\n%d",r[i]);
else printf("%d",r[i]);
return 0;
}