#include using namespace std; #define MAX 350000 int q, a; map > ans; struct frac { long long a, b; frac operator+(const frac& other) const { return {a + other.a, b + other.b}; } }; bool inter(long long x, frac a, frac b) { long long l = a.a * 1000000 / a.b; double t = a.a * (1.0 / a.b) * 1000000; if(t - l >= 0.5)l++; long long r = b.a * 1000000 / b.b; t = b.a * (1.0 / b.b) * 1000000; if(t - r >= 0.5)r++; return (l <= x && x <= r); } long long diff(long long x, frac a) { long long l = a.a * 1000000 / a.b; double t = a.a * (1.0 / a.b) * 1000000; if(t - l >= 0.5)l++; //cout << abs(x-l) << endl; return abs(x - l); } bool closest(long long x, frac a, frac b) { long long l = a.a * 1000000 / a.b; double t = a.a * (1.0 / a.b) * 1000000; if(t - l >= 0.5)l++; long long r = b.a * 1000000 / b.b; t = b.a * (1.0 / b.b) * 1000000; if(t - r >= 0.5)r++; if(abs(x - l) > abs(x - r))return false; return true; } int main() { freopen("fantasy.in", "r", stdin); freopen("fantasy.out", "w", stdout); ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> q; for(int i = 0; i < q; i++) { cin >> a; frac l{0, 1}; frac r{1, 1}; if(ans.count(a)) { cout << ans[a].first << " " << ans[a].second << endl; continue; } while(true) { frac mid = l + r; if(mid.a >= MAX || mid.b >= MAX)break; if(diff(a, l) == 0)break; if(diff(a, r) == 0)break; if(inter(a, l, mid))r = mid; else l = mid; //cout << l.a << " "<< l.b << " " << r.a << " "<< r.b << endl; // cout << mid.a << " " << mid.b << endl; } if(diff(a, l) == 0) { cout << l.a << " " << l.b << endl; ans[a] = {l.a, l.b}; } else { if(diff(a, r) != 0) { cout << "Golqm problem" << endl; exit(5396); } cout << r.a << " " << r.b << endl; ans[a] = {r.a, r.b}; } } return 0; }