1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> using namespace std;
typedef long long LL; int pri[]={2,3,5,7,11,13,17,19,23,29,31}; const LL INF = 1000000000000000000LL;
LL mult(LL a, LL b, LL mod){ LL ans = 0; a %= mod; while(b){ if(b&1){ ans += a; if(ans >= mod) ans -=mod; }b >>= 1; a <<= 1; if (a >= mod) a-= mod; }return ans; }
LL qpow(LL x, LL n, LL mod){ LL ans = 1; while(n){ if (n&1) ans = mult(ans ,x ,mod); n >>= 1; x = mult(x, x, mod); }return ans; }
LL gcd(LL a, LL b){ return b ? gcd(b, a % b) : a; }
bool witness(LL n, LL c){ LL s = qpow(c, n-1, n); if(s != 1) return false; LL p = n - 1; while(!(p&1) && s == 1){ p >>= 1; s = qpow(c, p, n); }if(s == 1 || s == n-1) return true; return false; }
bool miller_rabin(LL n){ if(n < 32){ for(int i = 0; i < 11; ++i) if(pri[i] == n) return true; return false; }for(int i = 1; i < 10; ++i) if(!witness(n, pri[i])) return false; return true; }
LL pollard_rho(LL n, LL c){ LL x = rand() % n, y = x, i = 1, k = 2, d; while(1){ i++; x = (mult(x, x, n) + c) % n; d =gcd( y - x + n, n); if(d > 1 && d < n) return d; if(y == x) return n; if(i == k){ k <<= 1; y = x; } } }
LL fac[3000]; int cnt; void find_factor(LL n){ if(miller_rabin(n)){ fac[++cnt] = n; return ; } LL p = n; while(p >= n) p = pollard_rho(n,rand() % (n - 1) + 1); find_factor(p); find_factor(n / p); }
LL ans1,ans2, nn, f[3000]; void dfs(LL sum, int step){ if(step == cnt + 1){ if(sum + nn / sum < ans1){ ans1 = sum + nn / sum; ans2 = sum; }return ; }dfs(sum *(LL)f[step], step + 1); dfs(sum, step + 1); }
int main(){ LL a, b; while(scanf("%lld %lld", &a, &b) != EOF){ if (a == b){ printf("%lld %lld\n",a, b); continue; } b /= a; nn = b; cnt = 0; find_factor(b); sort(fac + 1, fac + 1 + cnt); int t = cnt; cnt = 0; for(int i = 1; i <= t; ++i) if(fac[i] != fac[i - 1]) f[++cnt] = fac[i]; else f[cnt] *= fac[i]; ans1 = INF; dfs(1, 1); LL w = nn / ans2; if(w > ans2) swap(w, ans2); printf("%lld %lld\n",a * w, a * ans2); }return 0; }
|