Poj 2191 various prime number Algorithms

Source: Internet
Author: User
From discuss. analysis: first, the prime [I... n], and then use the miller_rabin prime number test method to test whether 2 ^ prime [I]-1 is a prime number (can be typed into a table or not, which does not affect much ). If not, use Pollard to perform qualitative factorization on the number. Books on the miller_rabin prime number test method and Pollard integer prime factor factorization number theory are available, almost as templates. Code: 2191 accepted 172 K 16 Ms C ++ 3050b # include <iostream> # include <cstring> # include <ctime> # include <cstdlib> # include <cmath> # include <algorithm> // # define max (POW (2.0, 62) # Define c 240 # define time 6 using namespace STD ;__ int64 ans [65]; // store 2 ^ prime [I]-1 prime factor int prime [65], X [65], M, CNT ;__ int64 gcd (_ int64, __int64 B) {If (B = 0) return a; return gcd (B, A % B) ;}__ int64 mod_mult (_ int64 A, __int64 B, __int64 N) {_ Int64 S = 0; A = A % N; while (B) {If (B & 1) {S + = A; If (S> = N) s-= N;} A = A <1; if (a> = n) A-= N; B = B> 1;} return s ;} __int64 mod_exp (_ int64 A, _ int64 B ,__ int64 N) {_ int64 d = 1; A = A % N; while (B> = 1) {If (B & 1) d = mod_mult (D, A, n); A = mod_mult (a, a, n); B = B> 1 ;} return D;} bool wintess (_ int64 A ,__ int64 N) {_ int64 M, X, Y; int I, j = 0; M = n-1; while (M % 2 = 0) {M = m> 1; j ++;} X = mod_exp (a, m, n); for (I = 1; I <= J; I ++) {Y = mod_exp (x, 2, n); If (y = 1) & (X! = 1) & (X! = N-1) return true; X = y;} If (y! = 1) return true; return false;} bool miller_rabin (INT times ,__ int64 N) {_ int64 A; int I; If (n = 1) return false; if (n = 2) return true; If (N % 2 = 0) return false; srand (Time (null); for (I = 1; I <= times; I ++) {A = rand () % (n-1) + 1; if (wintess (A, n) return false;} return true ;} __int64 Pollard (_ int64 N, int c) {_ int64 I, K, X, Y, D; srand (Time (null); I = 1; k = 2; X = rand () % N; y = x; while (true) {I ++; X = (mod_mul T (x, x, n) + C) % N; D = gcd (Y-X, n); If (D> 1 & D <n) return D; if (y = x) return N; if (I = k) {Y = x; k = k <1 ;}} void get_prime (_ int64 N, int c) {// finds all prime factor _ int64 m in binary mode; If (n = 1) return; If (miller_rabin (time, n )) {ans [CNT ++] = N; return;} M = N; while (M> = N) M = Pollard (M, c --); get_prime (m, c); get_prime (N/m, c);} void Init () {int I, TMP; memset (x, 0, sizeof (x )); X [0] = x [1] = 1; M = 0; for (I = 2; I <= 63; I ++) {If (! X [I]) {Prime [++ m] = I; TMP = I * I; while (TMP <= 63) {x [TMP] = 1; TMP + = I ;}}} bool CMP (_ int64 N1 ,__ int64 N2) {return N1 <N2 ;} int main () {Init (); int P, i, J; scanf ("% d", & P); for (I = 1; I <= m; I ++) {If (prime [I] <= P) {__ int64 n = (_ int64) 1 <prime [I])-1; if (miller_rabin (time, n) // determine whether it is a prime number continue; memset (ANS, 0, sizeof (ANS); CNT = 0; get_prime (N, C ); // factorization factor sort (ANS, ANS + CNT, CMP); For (j = 0; j <cnt-1; j ++) printf ("% i64d *", ans [J]); printf ("% i64d = % i64d = (2 ^ % d)-1 \ n", ANS [J], n, prime [I]);} elsebreak;} return 0 ;}

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.