Decomposition of large prime number templates (complexity less than sqrt (n))

Source: Internet
Author: User

POJ 1811#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <time.h>using namespace Std;typedef __int64 Lld;lld ran () {return rand () << | rand (); LLD gcd (LLD A, LLD b) {return!b? A:GCD (b, a% b);}    inline void Add (LLD &x, LLD ad, LLD MoD) {x + = AD; if (x >= mod) x-= mod;}    LLD Mul_mod (LLD A, LLD B, LLD MoD) {LLD ret = 0;        while (b) {if (b & 1) {Add (ret, a, mod); } b >>= 1;    Add (A, a, mod); } return ret;}    LLD Pow_mod (LLD x, LLD N, LLD MoD) {LLD ret = 1 mod;        while (n) {if (n & 1) {ret = Mul_mod (ret, x, MoD); } n >>= 1;    x = Mul_mod (x, x, mod); } return ret;}    BOOL Test (LLD N, LLD b) {LLD m = n-1;    int counter = 0;        while (~m & 1) {m >>= 1;    Counter + +;    } LLD ret = Pow_mod (b, M, n);    if (ret = = 1 | | ret = = n-1) {return true; } countER--;        while (counter >= 0) {ret = Mul_mod (ret, ret, n);        if (ret = = n-1) {return true;    } counter--; } return false;}    const int BASE[12] = {2,3,5,7,11,13,17,19,23,29,31,37};bool Is_prime (lld N) {if (n < 2) {return false;    } if (n < 4) {return true;    } if (n = = 3215031751LL) {return false;        } for (int i = 0; i < && base[i] < n; i++) {if (!test (n, Base[i])) {return false; }} return true;    LLD Pollard_rho (lld N, LLD seed) {LLD x, y, head = 1, tail = 2;    x = y = Ran ()% (n-1) + 1;        while (true) {x = Mul_mod (x, x, N);        Add (x, seed, N);        if (x = = y) {return n;        } LLD D = gcd (x > y? XY: Y-x, N);        if (1 < d && D < n) {return D;        } head + +;            if (head = = tail) {y = x;        Tail <<= 1; }}}vector &LT;lld> divisors;void factorize (lld N) {if (n > 1) {if (Is_prime (n)) {divisors.push_back (n);            }else {LLD d = n;            while (d >= n) {d = Pollard_rho (n, ran ()% (n-1) + 1);            } factorize (N/D);        Factorize (d);    }}}int Main () {//srand (Time (NULL));    int T;    scanf ("%d", &t);        for (int cas = 1; CAs <= T; cas++) {LLD x;        scanf ("%i64d", &x);        if (Is_prime (x)) {printf ("prime\n");            }else {divisors.clear ();            Factorize (x);            Sort (Divisors.begin (), Divisors.end ());        printf ("%i64d\n", Divisors[0]); }} return 0;}

  

Decomposition of large prime number templates (complexity less than sqrt (n))

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.