Nefu 120 Mason Prime number (--lucas-lehmer test judgment)

Source: Internet
Author: User

Mason Prime
problem:120 Time limit:1000ms Memory limit:65536k
Description
As Mason learned, talented, enthusiastic and the first systematic and deep study of the number of 2p-1 (of which p is a prime number), in order to commemorate him, the number of scholars called this number "Mason numbers", and the MP (which M is the first letter of the Mason name), namely Mp=2p-1. If Mason number is prime, it is called "Mason Prime". For example p=2,3,5,7, MP is prime, but 211-1 is not prime. Now ask you to find the first n of the number of Mason primes.
Input
There are multiple sets of test data. The first line is a positive integer t, which represents the number of groups of test data. Next to each set of 1 number p values, here 2<= p <= 62.
Output
For each set of test data, to determine whether the MP is the number of Mason, is the output "yes", no output "No", the output to be changed line.
Sample_input
227
Sample_output
Yesyes




Idea: Obviously n=2^p-1 when P is not a prime number, N is not a Mason, obviously not a prime (can decompose the proof of the type)

When P is prime, n is the number of Mason, it is necessary to determine whether n is prime, which can be determined by Lucas-lehmer test.


Knowledge Involved:

The Lucas-xylem test principle is this: make the Mason number mp = 2p1 as the test object (the preset p is the prime, otherwise Mp is the composite). Define sequence {si }: All i ≥0

if; , if.
.
.
.

The first few items of this sequence are 4, 14, 194, 37634, ... (Sequence A003010 in OEIS) then Mp is prime number and only if

Otherwise, Mp is composite. s P . 2 the remainder of modulo Mp is called the Lucas-xylem remainder of p .


Use the program to calculate the Sp-2 can, to pay attention to is p=2, can not use this method of determination, a special sentence can be


Accepted 800k 4ms C + + (g++ 3.4.3) 692 #include <cstdio> #include <iostream> #include <cstring> #include <algorithm>using namespace Std;typedef Long long ll;ll t,p;ll multi (ll a,ll b,ll MoD)//handwriting multiplication calculation, because the direct multiply will overflow long long{
   ll ans=0;    while (b)    {        if (b&1)            ans= (ans+a)%mod;        b>>=1;        A= (a<<1)%mod;    }    return ans;} int main () {    scanf ("%lld", &t);    while (t--)    {        scanf ("%lld", &p);        ll n= ((LL) 1<<p)-1;        ll r=4;        if (p==2) puts ("Yes"), and/or the other        {while            ((--p)!=1) r= (multi (r,r,n) -2+n)%n;            if (r%n==0) puts ("yes");            Else puts ("no");        }    }    return 0;}



Nefu 120 Mason Prime number (--lucas-lehmer test judgment)

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.