zoukankan      html  css  js  c++  java
  • Problem : [Noip2003]麦森数

    Problem : [Noip2003]麦森数

    Time Limit: 1 Sec Memory Limit: 128 MB

    Description

    形如2^P-1的素数称为麦森数,这时P一定也是个素数。但反过来不一定,即如果P是个素数,2P-1不一定也是素数。到1998年底,人们已找到了37个麦森数。最大的一个是P=3021377,它有909526位。麦森数有许多重要应用,它与完全数密切相关。任务:从文件中输入P(1000 < P < 3100000),计算2P-1的位数和最后500位数字(用十进制高精度数表示)

    Input

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

    Output

    第一行:十进制高精度数2^P-1的位数。
    第2-11行:十进制高精度数2^P-1的最后500位数字。(每行输出50位,共输出10行,不足500位时高位补0)
    不必验证2^P-1与P是否为素数。

    Sample Input

    1279

    Sample Output

    386
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000104079321946643990819252403273640855
    38615262247266704805319112350403608059673360298012
    23944173232418484242161395428100779138356624832346
    49081399066056773207629241295093892203457731833496
    61583550472959420547689811211693677147548478866962
    50138443826029173234888531116082853841658502825560
    46662248318909188018470682222031405210266984354887
    32958028878050869736186900714720710555703168729087
    code:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    void mul(int x[],int y[])
    {
        int tmp[520]={0},lx=500,ly=500,i,j,len;
        memset(tmp,0,sizeof(tmp));
        while(x[lx]==0&&lx>0) lx--; //计算x首位位置
        while(y[ly]==0&&ly>0) ly--; //计算y首位位置
        len=lx+ly;
        for(i=1;i<=ly;i++)
           for(j=1;j<=lx;j++)
               if(i+j-1<=500) tmp[i+j-1]+=y[i]*x[j];
        for(i=1;i<=500;i++) {tmp[i+1]+=tmp[i]/10; tmp[i]%=10; if(i<500&&tmp[i+1]==0) {len=i; break;}}
        for(i=500;i>0;i--) x[i]=tmp[i];
    }
    int main()
    {
        long p;
        int ans[501]={0},a[501]={0},i;
        scanf("%ld",&p);
        printf("%ld
    ",(long)floor(p*log10(2)+1));
        /*快速幂求2^p,同时用高精度乘法*/
        ans[1]=1; a[1]=2;
        while(p>0){
        if(p==1) {mul(ans,a); break;}
        else{
        if(p%2) mul(ans,a);
        p/=2;
        mul(a,a);}
        }
        ans[1]-=1;
        for(i=500;i>0;i--) {printf("%d",ans[i]); if((i-1)%50==0) printf("
    ");}
        //system("pause");
        return 0;
    }
    
  • 相关阅读:
    解决PKIX:unable to find valid certification path to requested target 的问题
    Linux 上的常用文件传输方式介绍与比较
    用VNC远程图形化连接Linux桌面的配置方法
    红帽中出现”This system is not registered with RHN”的解决方案
    linux安装时出现your cpu does not support long mode的解决方法
    CentOS SSH配置
    es6扩展运算符及rest运算符总结
    es6解构赋值总结
    tortoisegit安装、clon、推送
    es6环境搭建
  • 原文地址:https://www.cnblogs.com/ZhaoChongyan/p/11740420.html
Copyright © 2011-2022 走看看