vijos-p1223 Mason Number (high-precision multiplication + split + python)

Source: Internet
Author: User

P1223Mason Number accepted Tags: NOIP universal group 2003[Display label]<textarea id="code" class="textbox" style=""></textarea>Describe

The prime number of a shape such as 2^p-1 is called Mason, at which point P must also be a prime. But the reverse is not necessarily, that is, if P is a prime number, 2^p-1 is not necessarily prime. 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, calculate the number of bits of 2^p-1 and the last 500 digits (expressed in decimal high precision number)

Format input Format

Only one integer P (1000<p<3100000) is included in the file

Output format

First line: Decimal high-precision number of digits 2^p-1.

第2-11: The last 500 digits of the decimal high-precision number 2^p-1. (output 50 bits per line, output 10 lines, less than 500 bits when the high 0)
You do not have to verify that 2^p-1 and P are prime numbers.

Example 1 sample input 1[copy]
1279
Sample output 1[Copy]
3860000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001040 7932194664399081925240327364085538615262247266704805319112350403608059673360298012239441732324184842421613954281007791383 5662483234649081399066056773207629241295093892203457731833496615835504729594205476898112116936771475484788669625013844382 6029173234888531116082853841658502825560466622483189091880184706822220314052102669843548873295802887805086973618690071472 0710555703168729087
Limit

Each test point 1s

Tips

It's very simple, don't think it's complicated! ^_^

This topic, to use just one point, high-precision multiplication, if it is C + +, please use the method of partition, and for Java and Python, only need to directly call the function of high-precision calculation, here with the Python POW

The calculation of the number of bits is simple, 10^x + k = 2^p-1, log10 (2^p-k-1) = = x-> int (log10 (2) * p) + 1

Python code:


#!/usr/bin/env python3#-*-coding:utf-8-*-import mathimport sysp = Int (raw_input ()) print int (MATH.LOG10 (2) * P) + 1L =  Pow (2,p)-1L = l% pow (10,500) #将数字减少, otherwise the time of the remainder operation will increase, resulting in a timeout f = []for i in range:    f.append (L)    l/= 10for I In range (500-1,-1,-1):    sys.stdout.write ('%d '% f[i])    if i% = = 0:        print '        


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

vijos-p1223 Mason Number (high-precision multiplication + split + python)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.