zoukankan      html  css  js  c++  java
  • OpenJudge 2706 麦森数

    链接地址:http://bailian.openjudge.cn/practice/2706/

    题目:

    总时间限制:
    1000ms
    内存限制:
    65536kB
    描述
    形如2p-1的素数称为麦森数,这时P一定也是个素数。但反过来不一定,即如果P是个素数。2p-1不一定也是素数。到1998年底,人们已找到了37个麦森数。最大的一个是P=3021377,它有909526位。麦森数有许多重要应用,它与完全数密切相关。
    任务:从文件中输入P (1000<P<3100000) ,计算2p-1的位数和最后500位数字(用十进制高精度数表示)
    输入
    文件中只包含一个整数P(1000<P<3100000)
    输出
    第1行:十进制高精度数2p-1的位数。
    第2-11行:十进制高精度数2p-1的最后500位数字。(每行输出50位,共输出10行,不足500位时高位补0)
    不必验证2p-1与P是否为素数。
    样例输入
    1279
    样例输出
    386
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000104079321946643990819252403273640855
    38615262247266704805319112350403608059673360298012
    23944173232418484242161395428100779138356624832346
    49081399066056773207629241295093892203457731833496
    61583550472959420547689811211693677147548478866962
    50138443826029173234888531116082853841658502825560
    46662248318909188018470682222031405210266984354887
    32958028878050869736186900714720710555703168729087
    
    来源
    联赛复赛试题2003

    思路:

    这题自己写的方法超时了,参考了《程序设计引导及在线实践》的思路

    代码:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <math.h>
     4 #include <string.h>
     5 
     6 
     7 #define LEN 125
     8 
     9 void mul4(int *a,int *b)
    10 {
    11     int c[LEN];
    12     int temp;
    13     memset(c,0,sizeof(int)*LEN);
    14     for(int i = 0; i < LEN; i++)
    15     {
    16         temp = 0;
    17         for(int j = 0; j < LEN; j++)
    18         {
    19             if(i + j >= LEN) break;
    20             c[i + j] += a[i] * b[j] + temp;
    21             temp = c[i + j] / 10000;
    22             c[i + j] = c[i + j] % 10000;
    23         }
    24     }
    25     memcpy(a,c,LEN * sizeof(int));
    26 }
    27 
    28 int main()
    29 {
    30     int a[LEN],b[LEN];
    31     int i,j;
    32     memset(a,0,sizeof(int)*LEN);
    33     memset(b,0,sizeof(int)*LEN);
    34     b[0] = 2; a[0] = 1;
    35     int p;
    36     scanf("%d",&p);
    37     printf("%d
    ",(int)(p * log10(2)) + 1);
    38     while(p)
    39     {
    40         if(p & 1)
    41         {
    42             mul4(a,b);
    43         }
    44         p >>= 1;
    45         mul4(b,b);
    46     }
    47     a[0]--;
    48 
    49     char out_str[LEN * 4 + 1];
    50     for(i = 0; i < LEN; i++)
    51     {
    52         sprintf(&out_str[i*4],"%04d",a[LEN-1-i]);
    53     }
    54 
    55     for(i = 0;i < 10; i++)
    56     {
    57         for(j = 0; j < 50; j++)
    58         {
    59             printf("%c",out_str[i*50+j]);
    60         }
    61         printf("
    ");
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    Leetcode Reverse Words in a String
    topcoder SRM 619 DIV2 GoodCompanyDivTwo
    topcoder SRM 618 DIV2 MovingRooksDiv2
    topcoder SRM 618 DIV2 WritingWords
    topcoder SRM 618 DIV2 LongWordsDiv2
    Zepto Code Rush 2014 A. Feed with Candy
    Zepto Code Rush 2014 B
    Codeforces Round #245 (Div. 2) B
    Codeforces Round #245 (Div. 2) A
    Codeforces Round #247 (Div. 2) B
  • 原文地址:https://www.cnblogs.com/mobileliker/p/3516956.html
Copyright © 2011-2022 走看看