【高精度乘法】NOIP2003麦森数

题目描述

形如2^{P}-12P1的素数称为麦森数,这时PP一定也是个素数。但反过来不一定,即如果PP是个素数,2^{P}-12P1不一定也是素数。到1998年底,人们已找到了37个麦森数。最大的一个是P=3021377P=3021377,它有909526位。麦森数有许多重要应用,它与完全数密切相关。

任务:从文件中输入PP(1000<P<31000001000<P<3100000),计算2^{P}-12P1的位数和最后500位数字(用十进制高精度数表示)

输入输出格式

输入格式:

 

文件中只包含一个整数PP(1000<P<31000001000<P<3100000)

 

输出格式:

 

第一行:十进制高精度数2^{P}-12P1的位数。

第2-11行:十进制高精度数2^{P}-12P1的最后500位数字。(每行输出50位,共输出10行,不足500位时高位补0)

不必验证2^{P}-12P1与PP是否为素数。

 

输入输出样例

输入样例#1: 复制
1279
输出样例#1: 复制
386
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000104079321946643990819252403273640855
38615262247266704805319112350403608059673360298012
23944173232418484242161395428100779138356624832346
49081399066056773207629241295093892203457731833496
61583550472959420547689811211693677147548478866962
50138443826029173234888531116082853841658502825560
46662248318909188018470682222031405210266984354887
32958028878050869736186900714720710555703168729087

题解

裸的高精度。。。

就当打一遍板子。。。

代码

//by 减维
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<map>
#include<bitset>
#include<algorithm>
#define ll long long
using namespace std;

struct bignum{
    int a[1005];
    int num;
}a,b;

bignum operator * (const bignum&x,const bignum&y){
    bignum z;
    for(int i=0;i<=504;++i)z.a[i]=0;
    z.num=500;
    for(int i=1;i<=x.num;++i)
        for(int j=1;j<=y.num;++j)
        {
            int tmp=x.a[i]*y.a[j];
            tmp+=z.a[i+j-1];
            z.a[i+j-1]=tmp%10;
            z.a[i+j]+=tmp/10;
        }
        while(z.a[z.num]==0)z.num--;
    return z;
}

int p;

int main()
{
    scanf("%d",&p);
    printf("%d",(int)(log10(2)*p+1));
    a.a[1]=2;a.num=1;b.a[1]=1;b.num=1;
    a=a*b;
    while(p){
        if(p&1)b=(a*b);
        a=(a*a);
        p/=2;
    }
    b.a[1]--;
    for(int i=500;i>=1;--i){
        if(i%50==0)printf("\n");
        printf("%d",b.a[i]);
    }
}

 

posted @ 2017-10-27 19:57  减维  阅读(352)  评论(0编辑  收藏  举报