The problem is described as 2
P-1 of the prime number is called Mason, when p must also be a prime. But not necessarily, that is, if p is a prime number, 2
P-1 is not necessarily a prime number. By the end of 1998, 37 Mason had been found. The biggest one is p=3021377, which has 909,526 bits. Mason number has many important applications, which are closely related to the complete number.
Task: Enter P (1000<p<3100000) from the file and calculate 2
P-1 digits and last 500 digits (denoted by decimal high precision number) The input format file contains only one integer P (1000<p<3100000) output format the first line: decimal High Precision number 2
P-the number of digits in 1.
第2-11 line: Decimal High Precision number 2
P-the last 500 digits of 1. (output 50 bits per line, output 10 lines, less than 500 bits when the high 0)
No need to verify 2
P-whether 1 and p are prime numbers.
Thinking of solving problems: For the number of digits, there is a mathematical theorem for, int (log10 (2) *p+1) solves 2 of the P-square calculation method is as follows: 1, using the array as the data structure for high-precision calculation 2, using the two-part fast power to calculate the code as follows :
1#include <iostream>2#include <cmath>3#include <cstring>4 using namespacestd;5 inta[502]={0},b[502];6 voidcal () {7 inti,j,k;8memset (b,0,sizeof(b));9 for(i=1; i<= -; i++) {//corresponding multiplicationTen for(j=1, k=i;j<= -; j + +){ Oneb[k]=b[k]+a[i]*A[j]; Ak++; - if(i==501) Break;//take the No. 500 place on the break, the title requirements, no more useless. - } the } - for(i=1; i<= -; i++) {//Handling Rounding - if(b[i]>=Ten){ -b[i+1]+=b[i]/Ten; +b[i]=b[i]%Ten; - } +A[i]=b[i];//Update Fast Power A } at } - voidRecur (intp) { - if(p==1|| p==0){ -a[1]=2; - return; - } in Else{ -Recur (p/2); to cal (); + if(p%2==1){//and multiply the odd number by 2 -memset (b,0,sizeof(b)); the for(intI=1; i<= -; i++) {//corresponding multiplication *b[i]+=2*A[i]; $ }Panax Notoginseng } - for(intI=1; i<= -; i++) {//Handling Rounding the if(b[i]>=Ten){ +b[i+1]+=b[i]/Ten; Ab[i]=b[i]%Ten; the } +A[i]=b[i];//Update Fast Power - } $ } $ } - intMain () { - intp; theCin>>p; -cout<<int(LOG10 (2) *p+1);Wuyi recur (p); thea[1]-=1; - for(intI= -; I>0; i--){ Wu if(i% -==0){ -printf"\ n"); About } $printf"%d", A[i]); - - } - return 0; A}
Algorithm Training Mason Number