Poj1811_prime test [Miller Rabin prime number test] [pollar rov integer decomposition]

Source: Internet
Author: User
Prime testtime limit: 6000 ms memory limit: 65536 ktotal submissions: 29193 accepted: 7392 case time limit: 4000 msdescription


Given a big integer number, you are required to find out whether it's a prime number.
Input


The first line contains the number of test cases T (1 <= T <= 20 ), then the following T lines each contains an integer number N (2 <= n <2 ^ 54 ).
Output


For each test case, if n is a prime number, output a line containing the word "prime", otherwise, output a line containing the smallest prime factor of N.
Sample Input


2
5
10
Sample output


Prime
2
Source


Poj monthly

T-group data. If n is the prime number of input N, "prime" is output; otherwise, the minimum prime factor of N is output.

Idea: Because the N scale is 2 ^ 54, it is impossible to judge the common gender. Use the miller Rabin prime number test.

If n is not a prime number, we need to perform prime factor decomposition on N. Because N is a large number, it is considered to be decomposed using the pollar rov integer.


# Include <stdio. h> # include <stdlib. h> # include <time. h> # include <math. h> # define max_val (POW (2.0, 60) // miller_rabbin prime test // _ int64 mod_mul (_ int64 x ,__ int64 y ,__ int64 Mo) // {// _ int64 t; // X % = Mo; // For (t = 0; y; X = (x <1) % Mo, y> = 1) // If (Y & 1) // t = (t + x) % Mo; // return T; // }__ int64 mod_mul (_ int64 x ,__ int64 y ,__ int64 Mo) {_ int64 T, T, A, B, C, D, E, F, g, H, V, ans; t = (_ int64) (SQRT (double (Mo) + 0.5); t = T * T-Mo; A = x/T; B = x % t; C = y/T; D = Y % t; E = A * C/T; F = A * C % t; V = (A * D + B * C) % Mo + E * t) % Mo; G = V/T; H = V % t; ans = (F + G) * T % Mo + B * D) % Mo + H * t) % Mo; while (ANS <0) ans + = Mo; return ans; }__ int64 mod_exp (_ int64 num ,__ int64 t ,__ int64 Mo) {_ int64 ret = 1, temp = num % Mo; for (; t> = 1, temp = mod_mul (temp, temp, Mo) if (T & 1) ret = mod_mul (Ret, temp, Mo ); return ret;} bool Miller _ Rabbin (_ int64 N) {If (n = 2) return true; If (n <2 |! (N & 1) return false; int T = 0; _ int64 A, X, Y, u = n-1; while (U & 1) = 0) {T ++; U >>=1 ;}for (INT I = 0; I <50; I ++) {A = rand () % (n-1) + 1; X = mod_exp (A, U, N); For (Int J = 0; j <t; j ++) {Y = mod_mul (x, x, N ); if (y = 1 & X! = 1 & X! = N-1) return false; X = y;} If (X! = 1) return false;} return true;} // pollarrov big integer factorization _ int64 minfactor ;__ int64 gcd (_ int64 A ,__ int64 B) {If (B = 0) return a; return gcd (B, A % B) ;}__ int64 pollarrov (_ int64 N, int c) {int I = 1; srand (Time (null); _ int64 x = rand () % N; _ int64 y = x; int K = 2; while (true) {I ++; X = (mod_exp (x, 2, n) + C) % N; _ int64 d = gcd (Y-X, N ); if (1 <D & D <n) return D; If (y = x) return N; if (I = k) {Y = X; K * = 2 ;}} void getsmallest (_ int64 N, int c) {If (n = 1) return; If (miller_rabbin (n )) {If (n <minfactor) minfactor = N; return;} _ int64 val = N; while (val = N) val = pollarrov (N, C --); getsmallest (Val, c); getsmallest (N/Val, c);} int main () {int t; _ int64 N; scanf ("% d ", & T); While (t --) {scanf ("% i64d", & N); minfactor = max_val; If (miller_rabbin (n )) printf ("prime \ n"); else {getsmallest (n, 200); printf ("% i64d \ n", minfactor) ;}} return 0 ;}


Poj1811_prime test [Miller Rabin prime number test] [pollar rov integer decomposition]

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.