HDU 3864 d_num Miller Rabin prime number judgment + Pollard ROV big integer Decomposition

Source: Internet
Author: User

Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3864

A number N (1 <= n <10 ^ 18) is given. If n has only four dikes, three dikes except 1 are output.

Idea: the prime factor decomposition of large numbers can only be performed using random algorithms Miller Rabin and pollard_rock. The accuracy is guaranteed when there are many tests.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <map> # include <cstdlib> # include <queue> # include <stack> # include <vector> # include <ctype. h> # include <algorithm> # include <string> # include <set> # include <ctime> # define PI ACOs (-1.0) # define INF 0x7fffffff # define EPS 1e-8 # define maxn 50005 typedef _ int64 ll; typedef unsigned long ull; using namespace STD; ll factor [100]; int T = 0; ll mul_mod (ll a, LL B, ll N) {A = A % N; B = B % N; ll s = 0; while (B) {If (B & 1) S = (S + a) % N; A = (a <1) % N; B = B> 1 ;}return s ;} ll pow_mod (ll a, LL B, ll N) // evaluate a ^ B % N {A = A % N; ll S = 1; while (B) {If (B & 1) S = mul_mod (s, A, n); A = mul_mod (a, a, n); B = B> 1 ;} return s;} bool isprime (ll n, ll times) {If (n = 2) return 1; if (n <2 |! (N & 1) return 0; ll A, u = n-1, X, Y; int T = 0; while (U % 2 = 0) {T ++; u/= 2;} srand (100); For (INT I = 0; I <times; I ++) {A = rand () % (n-1) + 1; X = pow_mod (A, U, N); For (Int J = 0; j <t; j ++) {Y = mul_mod (x, x, N ); if (y = 1 & X! = 1 & X! = N-1) return false; // must not X = y;} If (y! = 1) return false;} return true;} ll gcd (ll a, LL B) {if (a = 0) return 1; if (a <0) return gcd (-a, B); return B = 0? A: gcd (B, A % B);} ll pollard_rov (ll n, ll c) // pollard_rov algorithm, find the N factor {ll I = 1, J, k = 2, X, Y, D, P; X = rand () % N; y = x; while (true) {I ++; X = (mul_mod (X, x, n) + C) % N; If (y = x) return N; If (Y> X) P = Y-X; else P = x-y; D = gcd (p, n); If (D! = 1 & D! = N) return D; if (I = k) {Y = x; k + = K ;}} void factor (LL N) {If (isprime (n, 20) {factor [t ++] = N; return;} ll p = N; while (P> = N) P = pollard_rock (p, Rand () % (n-1) + 1); factor (p); factor (N/P);} void solve (ll a) {if (a = 1) {printf ("is not a d_num \ n"); return;} t = 0; factor (a); sort (factor, factor + t ); if (t = 2) {If (factor [0]! = Factor [1]) {printf ("% i64d % i64d % i64d \ n", factor [0], factor [1], );} else printf ("is not a d_num \ n");} else if (t = 3) {If (factor [0] = factor [1] & factor [1] = factor [2]) printf ("% i64d % i64d % i64d \ n ", factor [0], factor [0] * factor [1], a); else printf ("is not a d_num \ n ");} else printf ("is not a d_num \ n");} int main () {ll A; while (~ Scanf ("% i64d", & A) {solve (a) ;}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.