Miller-Rabin + pollard-rock Template

Source: Internet
Author: User

For details, see Introduction to algorithms.

HDU 4344

1. The length is a factor of N (and greater than 1 and less than N), and the elements in the set have mutual quality.

2. to make as many choices as possible, each quality factor of l should contain only one quality factor of N, and L is an integer of a quality factor, so the value of K is the number of different quality factors in N.

3. If you want to maximize the value of each l, you only need to maximize the index of the quality factor.

Therefore, we use pollard_rov to break down the prime factor of N, then count the number of different prime factors k, and calculate the products of all the same prime factor and S.

Special case: If n is an integer I power of a prime number, then K can only be equal to 1, because l is smaller than N, so l is the maximum I-1 power of this prime number

This template was used in poj before... On this question, WA is so explosive... I finally found that I did not pay attention to the above situation...

Spit it out. The disgusting hdoj cannot use LLD ....!!

 

# Include <iostream> # include <cstdio> # include <cmath> # include <vector> # include <cstring> # include <algorithm> # include <string> # include <set> # include <ctime> # include <queue> # include <map> # include <sstream> # define Cl (ARR, val) memset (ARR, (VAL), sizeof (ARR) # define rep (I, n) for (I) = 0; (I) <(N ); ++ (I) # define for (I, L, H) for (I) = (l); (I) <= (h ); ++ (I) # define Ford (I, H, L) for (I) = (h); (I)> = (L); -- (I) # define L (x) <1 # define R (x) <1 | 1 # define mid (L, r) (l) + (R)> 1 # define min (x, y) (x) <(y )? (X): (y) # define max (x, y) (x) <(y )? (Y): (x) # define e (x) (1 <(x) # define iabs (x)> 0? (X):-(x) typedef long ll; const double EPS = 1e-8; // const int INF = ~ 0u> 2; using namespace STD; const int c = 16381; const ll INF = 1 <22; Map <LL, ll> MP; ll gcd (ll, LL B) {return B? Gcd (B, A % B): A;} ll mod_mul (ll a, LL B, ll N) {ll res = 0; while (B> 0) {If (B & 1) {res = (RES + a); If (RES> = N) RES-= N;} A = (a + ); if (A> = n) A-= N; B> = 1;} return res;} ll mod_exp (ll a, LL B, ll N) {ll res = 1; while (B> 0) {If (B & 1) RES = mod_mul (Res, A, n); A = mod_mul (a,, n); B> = 1;} return res;} bool miller_rabin (LL N) {If (n = 2 | n = 3 | n = 5 | n = 7 | n = 11) retur N true; If (n = 1 |! (N % 2) |! (N % 3) |! (N % 5) |! (N % 7) |! (N % 11) return false; ll X, pre, U; int I, j, k = 0; u = n-1; while (! (U & 1) {k ++; U >>= 1 ;}srand (LL) time (0); for (I = 0; I <5; + + I) {// 5 times enough ac x = rand () % (n-2) + 2; If (X % n) = 0) continue; X = mod_exp (x, u, n); Pre = x; For (j = 0; j <K; ++ J) {x = mod_mul (X, X, n); If (x = 1 & Pre! = 1 & Pre! = N-1) return false; Pre = x;} If (X! = 1) return false;} return true;} ll pollard_rov (ll n, ll c) {ll X, Y, D; ll I, K; I = 1; k = 2; srand (LL) time (0); X = rand () % (n-1) + 1; y = x; while (1) {I ++; X = (mod_exp (x, 2, n) + C) % N; D = gcd (Y-x + n, n); If (D! = 1 & D! = N) return D; If (x = y) return N; if (I = k) {Y = x; K * = 2 ;}}} void factor (ll n, ll c) {If (n <= 1) return; ll R, D; If (miller_rabin (n )) {If (MP [N] = 0) MP [N] = N; else MP [N] * = N; return;} r = pollard_rov (N, C --); D = N/R; factor (D, C); factor (R, c);} int main () {// freopen ("data. in "," r ", stdin); int t; ll N, K, sum; scanf (" % d ", & T); While (t --) {CIN> N; If (miller_rabin (N) {cout <1 <<"" <1 <Endl;} else {sum = 0; k = 0; MP. clear (); factor (N, C); Map <LL, ll >:: iterator it; for (IT = MP. begin (); it! = MP. end (); ++ it) {k ++; sum + = it-> second;} It = MP. begin (); If (k = 1) cout <k <"" <sum/IT-> first <Endl; else cout <k <"" <sum <Endl ;}} 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.