1469D - Ceil Divisions

Editorial.

Time complexity: $O(n)$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
inline int ceil(int a, int b) { return (a + b - 1) / b; }
int bs(int l, int r, function<bool(int)> p)
{
    while (l < r)
    {
        int m = l + (r - l) / 2;
        if (p(m))
            r = m;
        else
            l = m + 1;
    }
    return l;
}
void solve(int n, vector<ii> &ans)
{
    if (n == 2)
        return;
    int i = bs(1, n - 1, [n](int x) { return x >= ceil(n, x); });
    for (int j = i + 1; j < n; ++j)
        ans.emplace_back(j, n);
    ans.emplace_back(n, i);
    ans.emplace_back(n, i);
    solve(i, ans);
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<ii> ans;
        solve(n, ans);
        cout << ans.size() << endl;
        for (auto [x, y] : ans)
            cout << x << " " << y << endl;
        cout << endl;
    }
    return 0;
}