Miller-rabin algorithm Codevs 1702 prime number Determination 2

Source: Internet
Author: User

Reprinted from: http://www.dxmtb.com/blog/miller-rabbin/

We have an O (√n) test algorithm for ordinary prime numbers. In fact, we have an O (slog³n) algorithm.

Theorem One: If P is a prime number and (a,p) = 1, then a^ (p-1) ≡1 (mod p). That is, if p is a prime number and a,p coprime, then the remainder of a (p-1) divided by P is constant equal to 1. (Fermat theorem)

The inverse proposition of the theorem is not necessarily tenable, but it is gratifying that most cases are established.

So we get a direct application of the theorem, for the number of p to be verified, we continue to take a∈[1,p-1] and a∈z, verify that a^ (p-1) mod p is equal to 1, not p decisive is not prime, a total of s times. where a^ (p-1) mod p can be written by the p-1 binary, by (a*b) mod c= (a mod c) *b mod c, can be calculated in T=log (p-1) time, such as the complexity of integer multiplication, the total complexity of a calculation is log³ (p-1). This method is called fast power modulo.

In order to improve the accuracy of the algorithm, we have another theorem that can be used.
Theorem two: for 0<x<p,x^2 mod p =1 = x=1 or p-1.

We make p-1= (2^t) *u, that is, the p-1 is the U binary representation followed by the T 0. We first calculate the X[0]=a^u mod p, then the square T times and at each time modulo p, each time the results are recorded as X[i], and finally can be calculated a^ (p-1) mod p. If found X[i]=1 and x[i-1] is not equal to 1 is not equal to p-1, then found that P decisive is not prime.

It can be proved that after using the above two theorems, the probability of testing s error is at most 2^ (-s), so the algorithm is very reliable.

It is important to note that in order to prevent overflow (particularly large data), A*b mod C also uses a method similar to fast power modulo. Of course, the data is not very large can be waived

Codevs 1702 Prime number Determination 2time limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle Description Description

A number, is he a prime?

Set him up for P satisfaction (p<=2^63-1)

Enter a description Input Description

P

Output description Output Description

yes| No

Sample input Sample Input

2

Sample output Sample Output

Yes

Data range and Tips Data Size & Hint

An introduction to Algorithms--the section of number theory
Note Carmichael number

Category labels Tags Click here to expandNumber theory analysis: Because the data range is 2^63-1 large, the general method time complexity is very high, so using the stochastic algorithm--miller Rabin algorithm
1 /*The only thing I want to say is that the data on this topic is wrong.*/2 #defineS 153#include <iostream>4 using namespacestd;5#include <cstdio>6 #definell Long Long7#include <cstdlib>8#include <ctime>9 ll Quick_mod (ll x,ll u,ll N)Ten { Onex%=N; All ans=1; -      while(U) -     { the         if(u&1) -         { -u--; -ans= (ans*x)%N; +         } -u>>=1; +x= (x*x)%N; A     } at     returnans; - } - BOOLMiller_rabin (ll N) - { -     if(n==2)return true; -     if(n==1||! (n&1))return false; inll u=n-1; -ll t=0; to      while(! (u&1)) +     { -t++; theu>>=1; *     } $      for(intI=0; i<s;++i)Panax Notoginseng{/*Here you can randomly find x, or you can hand-hit a table, int ss[7]={2,3,5,7,11,13,17}; it turns out that the table is correct within a long long range .*/ -ll X=rand ()% (n1)+1; thex=Quick_mod (x,u,n); +          for(intI=1; i<=t;++i) A         { thell Y=quick_mod (x,2, n); +             if(y==1&&x!=1&&x!=n-1) -               return false; $x=y; $         } -         if(x!=1) -           return false; the     } -     return true;Wuyi } the intMain () - { WuSrand (Time (0)); - ll N; AboutCin>>N; $     if(Miller_rabin (n)) -printf"Yes"); -     Elseprintf"No"); -     return 0; A}

Miller-rabin algorithm Codevs 1702 prime number Determination 2

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.