#include <bits/stdc++.h>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
//#include <time.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
//#define M ll(1e9 + 7)
#define M ll(998244353)
#define sz(x) (int)x.size()
#define re return
#define oo ll(1e18)
#define el '\n'
#define pii pair <int, int>
#define all(x) (x).begin(), (x).end()
#define arr_all(x, n) (x + 1), (x + 1 + n)
#define vi vector<int>
#define eps (ld)1e-9
using namespace std;
typedef long long ll;
//using namespace __gnu_pbds;
//typedef tree <ll, null_type, less_equal <ll> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef double ld;
typedef unsigned long long ull;
typedef short int si;
const int N = 2e5 + 500;
int g, h;
ll r(ll x, ll y) {
if (x > y) {
swap(x, y);
}
if (x == 1)
return y;
// cerr << x << " " << y << el;
return r(x, y / x);
}
bool gcd(ll x, ll y, ll d) {
if (d > 3)
return 0;
if (x > y) {
swap(x, y);
}
if (x != 0 && r(x, y) == h) {
cout << x << " " << y << el;
return 1;
}
for (int a = 20; a >= 10; a--) {
bool can = gcd(y * a + x, y, d + 1);
if (can)
return 1;
}
return 0;
}
bool gd(ll a, ll b) {return (b % a == 0);}
void solve() {
cin >> g >> h;
if (g == h) {
cout << g << " " << g << el;
return;
}
if (g <= 2000 && h <= 2000) {
gcd(0, g, 0);
return;
}
if (g < h) {
ll st = g * 1ll * h * ((h + g - 1) / g);
ll hop = st;
ll a = st;
ll b = st * 2ll + g;
while (1) {
ll R = r(a, b);
if (R == h) {
break;
}
if (R == st) {
assert(0);
break;
}
b += hop;
}
//
cout << a << " " << b << el;
return;
}
for (ll G = g; G > 0; G *= g) {
ll st = G;
ll hop = st;
ll a = st;
ll b = st * 2ll + ((G > g) ? g : 0);
while (1) {
ll R = r(a, b);
if (R == h) {
break;
}
if (R == g) {
break;
}
b += hop;
}
if (r(a, b) == h) {
cout << a << " " << b << el;
// cout << __gcd(a, b) << " " << r(a, b) << el;
return;
}
}
}
int main()
{
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());;
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// in("toys.in");
// out("toys.out");
// in("input.txt");
// out("output.txt");
// cerr.precision(9); cerr << fixed;
// clock_t tStart = clock();
//1048576
// for (ll i = 1048576; i <= 1e10; i += 1048576) {
// cout << 1048576 << " " << i << " -> " << r(1048576, i) << el;
// }
int q;
cin >> q;
while (q--) {
solve();
}
}
/*
7
4 6 7 2 3 1 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |