NEFU120 Mason Prime number "Lucas-lehmer judgment"

Source: Internet
Author: User

Topic Links:

http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=120


Main topic:

Give a number p, if MP = 2^p-1 is a mason prime, then output "yes", otherwise output "no".


Ideas:

This problem p is less than 62. MP is less than 2^62-1. If the direct number of primes will be relatively slow, using the Sieve method of the amount of data

It's going to be big. So here is the rule of lucas-lehmer judgment.

Lucas-lehmer decision rule Specific steps:

If you want to determine the integer MP = 2^p-1, make lucas sequence date[1] = 4,l (i) = (L (i-1) ^2-2)% MP,

If date[p-1] = = 0, then the number of MP is Mason Prime. In particular, the first Mason prime number 2 is to be specially sentenced.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    Std;long Long Date[70];long long Multi (Long long A,long long B,long long m)//a * B% m{long long ret = 0;        while (b) {if (b & 1) ret = (ret + a)% m;        b >>= 1;    A = (a << 1)% m; } return ret;}    int main () {Long long sum,temp;    int t,n;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        sum = 1;        sum = (sum<<n);        Sum-= 1;        DATE[1] = 4;            for (int i = 2; I <= N-1; ++i) {temp = Multi (date[i-1],date[i-1],sum);        Date[i] = (temp-2)% sum;        } if (N = = 2) printf ("yes\n");            else {if (date[n-1] = = 0) printf ("yes\n");        else printf ("no\n"); }} return 0;}


NEFU120 Mason Prime number "Lucas-lehmer 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.