P1045 麦森数

题目描述

形如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;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值