Code:
# Include <cstdio> # include <cstdlib> # include <ctime> # include <cstring> # include <iostream> # include <algorithm> const int S = 20; using namespace STD; typedef long ll; # define maxn sort ll factor [maxn]; int tot; ll muti_mod (ll a, LL B, ll c) {// return (A * B) moD C, A, B, C <2 ^ 63 A % = C; B % = C; ll ret = 0; while (B) {If (B & 1) {RET + = A; If (Ret> = C) ret-= C;} A <= 1; if (a> = C) a-= C; b> = 1;} return ret;} ll pow_mod (LL X, ll N, LL MoD) {// returns x ^ n mod C, non-recursive if (n = 1) return X % MOD; int bit [64], K = 0; while (n) {bit [k ++] = N & 1; n >>=1;} ll ret = 1; for (k = K-1; k> = 0; k --) {ret = muti_mod (Ret, RET, MoD); If (bit [k] = 1) ret = muti_mod (Ret, X, MoD );} return ret;} bool check (ll a, ll N, ll X, ll t) {// use A as the base, n-1 = x * 2 ^ t, check whether N is a combination of LL ret = pow_mod (A, X, n), last = ret; For (INT I = 1; I <= T; I ++) {ret = muti_mod (Ret, RET, n); If (ret = 1 & last! = 1 & last! = N-1) return 1; last = ret;} If (Ret! = 1) return 1; return 0;} bool miller_rabin (ll n) {ll x = n-1, T = 0; while (X & 1) = 0) x> = 1, t ++; bool flag = 1; if (T> = 1 & (X & 1) = 1) {for (int K = 0; k <s; k ++) {ll A = rand () % (n-1) + 1; if (check (A, n, x, t) {flag = 1; break;} flag = 0 ;}} if (! Flag | n = 2) return 0; return 1;} ll gcd (ll a, LL B) {if (a = 0) return 1; if (A <0) return gcd (-a, B); while (B) {ll T = A % B; A = B; B = T;} return ;} ll pollard_rov (ll x, ll c) {ll I = 1, X0 = rand () % x, y = x0, K = 2; while (1) {I ++; x0 = (muti_mod (x0, x0, x) + C) % x; ll d = gcd (y-x0, x); If (D! = 1 & D! = X) {return D;} If (y = x0) return X; if (I = k) {Y = x0; k + = K ;}}} void findfac (ll n) {// recursive prime factor decomposition n if (! Miller_rabin (N) {factor [tot ++] = N; return;} ll p = N; while (P> = N) P = pollard_rov (p, Rand () % (n-1) + 1); findfac (p); findfac (N/P);} int main () {srand (Time (null); int T; scanf ("% d", & T); While (t --) {ll N; scanf ("% LLD", & N); If (! Miller_rabin (N) {// printf ("prime \ n"); cout <n <Endl ;}else {tot = 0; findfac (N ); sort (factor, factor + ToT); For (INT I = 0; I <tot; I ++) printf ("% LLD", factor [I]); printf ("\ n") ;}} return 0 ;}
Method 2:
# Include <cstdio> # include <cstdlib> # include <ctime> # include <cstring> # include <iostream> # include <algorithm> const int S = 20; using namespace STD; typedef long ll; # define maxn sort ll factor [maxn]; int tot; ll muti_mod (ll a, LL B, ll c) {// return (A * B) moD C, A, B, C <2 ^ 63 A % = C; B % = C; ll ret = 0; while (B) {If (B & 1) {RET + = A; If (Ret> = C) ret-= C;} A <= 1; if (a> = C) a-= C; b> = 1;} return ret;} ll pow_mod (LL X, ll N, LL MoD) {// return x ^ n mod C ll ret = 1; while (n) {If (N & 1) ret = muti_mod (Ret, X, MoD ); X = muti_mod (x, x, MoD); n = n> 1;} return ret;} bool check (ll a, ll N, ll X, ll T) {// Based on A, n-1 = x * 2 ^ t, check whether N is a combination of LL ret = pow_mod (A, X, n), last = ret; for (INT I = 1; I <= T; I ++) {ret = muti_mod (Ret, RET, n); If (ret = 1 & last! = 1 & last! = N-1) return 1; // n is the sum of last = ret;} If (Ret! = 1) return 1; return 0;} bool miller_rabin (ll n) {ll x = n-1, T = 0; while (X & 1) = 0) x> = 1, t ++; bool flag = 0; // flag is 0, indicating the sum, 1 indicating the prime number if (T> = 1 & (X & 1) = 1) {// when n is an even number, it must not be a prime number (except for 2) for (int K = 0; k <s; k ++) {ll A = rand () % (n-2) + 1; if (check (A, n, x, t) {flag = 0; break ;} flag = 1 ;}}if (flag | n = 2) return 1; return 0;} ll gcd (ll a, LL B) {if (a = 0) return 1; if (a <0) return gcd (-a, B); while (B) {ll T = A % B; A = B; B = T;} return A;} ll pollard_rov (ll x, ll c) {ll I = 1, X0 = rand () % x, y = x0, K = 2; while (1) {I ++; X0 = (muti_mod (x0, x0, x) + C) % x; ll d = gcd (y-x0, x); If (D! = 1 & D! = X) {return D;} If (y = x0) return X; if (I = k) {Y = x0; k + = K ;}}} void findfac (ll n) {// recursive prime factor decomposition n if (miller_rabin (N) {factor [tot ++] = N; return;} ll p = N; while (P> = N) P = pollard_rov (p, Rand () % (n-1) + 1); findfac (p); findfac (N/P );} int main () {srand (Time (null); int t; scanf ("% d", & T); While (t --) {ll N; scanf ("% LLD", & N); If (miller_rabin (N) {// printf ("prime \ n"); cout <n <Endl ;} else {tot = 0; findfac (n); sort (factor, factor + ToT); For (INT I = 0; I <tot; I ++) printf ("% LLD", factor [I]); printf ("\ n") ;}} return 0 ;}