2411: 【普及/提高-】【P1045】麦森数

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:1 解决:1

题目描述

形如 lns="http://www.w3.org/1998/Math/MathML">21 的素数称为麦森数,这时 lns="http://www.w3.org/1998/Math/MathML"> 一定也是个素数。但反过来不一定,即如果 lns="http://www.w3.org/1998/Math/MathML"> 是个素数,lns="http://www.w3.org/1998/Math/MathML">21 不一定也是素数。到 1998 年底,人们已找到了 37 个麦森数。最大的一个是 lns="http://www.w3.org/1998/Math/MathML">=3021377,它有 909526 位。麦森数有许多重要应用,它与完全数密切相关。

任务:输入 lns="http://www.w3.org/1998/Math/MathML">(1000<<3100000),计算 lns="http://www.w3.org/1998/Math/MathML">21 的位数和最后 lns="http://www.w3.org/1998/Math/MathML">500 位数字(用十进制高精度数表示)

输入

文件中只包含一个整数 lns="http://www.w3.org/1998/Math/MathML">(1000<<3100000)

输出

第一行:十进制高精度数 lns="http://www.w3.org/1998/Math/MathML">21 的位数。

第 lns="http://www.w3.org/1998/Math/MathML">211 行:十进制高精度数 lns="http://www.w3.org/1998/Math/MathML">21 的最后 lns="http://www.w3.org/1998/Math/MathML">500 位数字。(每行输出 lns="http://www.w3.org/1998/Math/MathML">50 位,共输出 lns="http://www.w3.org/1998/Math/MathML">10 行,不足 lns="http://www.w3.org/1998/Math/MathML">500 位时高位补 lns="http://www.w3.org/1998/Math/MathML">0

不必验证 lns="http://www.w3.org/1998/Math/MathML">21 与 lns="http://www.w3.org/1998/Math/MathML"> 是否为素数。

样例输入 复制

1279

样例输出 复制

386
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000104079321946643990819252403273640855
38615262247266704805319112350403608059673360298012
23944173232418484242161395428100779138356624832346
49081399066056773207629241295093892203457731833496
61583550472959420547689811211693677147548478866962
50138443826029173234888531116082853841658502825560
46662248318909188018470682222031405210266984354887
32958028878050869736186900714720710555703168729087

提示

NOIP 2003 普及组第四题

#include <iostream>
#include <cmath>
using namespace std;
long long a[501]={0};
long long cp(int n)
{
    long long result=1;
    for (int i = 1; i <= n; i++)
    {
        result*=2;
    }
    return result;
}
int main()
{
    int p;
    cin>>p;
    int num1=p/32;
    int num2=p-num1*32;
    long long x=cp(32);
    a[500]=1;
    /**
     * @brief 
     * 为了加快速度,我们在前num1轮对每个数乘以2^32,由于p/32并不一定刚好整除,因此我们在剩下的num1轮当中对每个数每次乘2
     */
    for (int i = 1; i <= num1; i++)
    {
        for (int j = 500; j >= 1; j--)
        {
            a[j]*=x;
        }
        for (int j = 500; j >= 1; j--)
        {
            a[j-1]+=a[j]/10;
            a[j]%=10;
        }
    }
    for (int i = 1; i <= num2; i++)
    {
        for (int j = 500; j >= 1; j--)
        {
            a[j]*=2;
        }
        for (int j = 500; j >= 1; j--)
        {
            a[j-1]+=a[j]/10;
            a[j]%=10;
        }
    }
    a[500]--;
    cout<<(int)(p*log10(2)+1)<<endl;
    for (int i = 1; i <= 500; i++)
    {
        cout<<a[i];
        if (i%50==0)
        {
            cout<<endl;
        }
    }
}