First, the LCM/GCD is decomposed. The problem is to select a number from the factor to multiply, and the remaining number is also multiplied, with the minimum and minimum requirements.
You can directly search here. Pay attention to one problem. Because the same factor cannot be assigned to both sides (which will change gcd), you can merge the same factor. In this way, the number of layers of the search will also decrease.
# Include <stdio. h> # include <string. h> # include <iostream> # include <math. h> # include <stdlib. h> # include <time. h >#include <algorithm> using namespace STD; typedef long ll; # define maxn sort ll factor [maxn]; int tot; const int S = 10; // number of tests ll muti_mod (ll a, LL B, ll c) {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 ){ If (n = 1) return X % MOD; int bit [90], 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) {// 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; last = ret;} If (Ret! = 1) return 1; return 0;} bool miller_rabin (ll n) {// returns 0 for a prime number, returns 1 LL x = n-1 for a sum, and T = 0 for a sum; 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 (, 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) // prime factor decomposition. {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);} ll mins, AA, BB; int top; void DFS (ll a, ll B, int p) {if (a + B> = mins) return; If (P = Top) {if (a + B <mins) {mins = A + B; AA = A; BB = B;} return;} DFS (A * factor [p], B, P + 1); DFS (A, B * factor [p], P + 1);} int main () {ll A, B, C; while (~ Scanf ("% LLD", & A, & B) {if (a = B) {printf ("% LLD \ n",, b); continue;} mins = ~ 0ull> 1; C = B/a; Tot = 0; findfac (c); sort (factor, factor + ToT); Top = 0; For (INT I = 0; I <tot; I ++) {if (I = 0) factor [top ++] = factor [I]; else if (factor [I] = factor [I-1]) factor [Top-1] * = factor [I]; else factor [top ++] = factor [I];} DFS (a, a, 0); If (AA> BB) Swap (AA, BB ); printf ("% LLD \ n", AA, BB);} return 0 ;}