题目描述
形如2{P}-12P−1的素数称为麦森数,这时PP一定也是个素数。但反过来不一定,即如果PP是个素数,2{P}-12P−1不一定也是素数。到1998年底,人们已找到了37个麦森数。最大的一个是P=3021377P=3021377,它有909526位。麦森数有许多重要应用,它与完全数密切相关。
任务:从文件中输入PP(1000<P<31000001000<P<3100000),计算2^{P}-12P−1的位数和最后500位数字(用十进制高精度数表示)
输入格式
文件中只包含一个整数PP(1000<P<31000001000<P<3100000)
输出格式
第一行:十进制高精度数2^{P}-12P−1的位数。
第2-11行:十进制高精度数2^{P}-12P−1的最后500位数字。(每行输出50位,共输出10行,不足500位时高位补0)
不必验证2^{P}-12P−1与PP是否为素数。
输入输出样例
输入 #1
1279
输出 #1
386
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000104079321946643990819252403273640855
38615262247266704805319112350403608059673360298012
23944173232418484242161395428100779138356624832346
49081399066056773207629241295093892203457731833496
61583550472959420547689811211693677147548478866962
50138443826029173234888531116082853841658502825560
46662248318909188018470682222031405210266984354887
32958028878050869736186900714720710555703168729087
思路
- 对于位数由于2^p 的位数和2^p-1 肯定是一样的,所以位数就是\lfloor log10(2^p)\rfloor+1,p*\lfloor log10(2)\rfloor+1
- 双高精度乘法
- 快速幂
#include<iostream>
#include<cmath>
using namespace std;
const int N = 510;
int a[N],c[N],res[N];
int p;
void mul(int a[],int b[],int res[]){
for(int i=0;i<500;++i)c[i]=0;
for(int i=0;i<500;++i){
for(int j=0;j<500;++j){
if(i+j<500){
c[i+j]+=a[i]*b[j];
}
}
}
int t=0;
for(int i=0;i<500;++i){
t+=c[i];
res[i]=t%10;
t/=10;
}
}
void qmi()
{
a[0]=2;
res[0]=1;
while(p){
if(p&1)mul(a,res,res);
p>>=1;
mul(a,a,a);
}
res[0]--;
}
int main()
{
cin>>p;
cout<<(int)(p*log10(2))+1<<endl;
qmi();
int k=0;
for(int i = 500-1;i>=0;--i){
++k;
printf("%d",res[i]);
if(k%50==0)
puts("");
}
}