Find the law
Can be found in the first position is a b x, a second position b in the last, it is the lexicographic order $ x * (x-1) / 2 $
Thus solving equations $ x * (x-1) / 2 = n $
#include<iostream> #include<cmath> using namespace std; typedef long long ll; int main() { int t; cin >> t; while (t--) { ll n; ll k; cin >> n >> k; ll x = (1 + sqrt(1 + 8 * k)) / 2; ll y = k - x * (x - 1) / 2; if (y) ++x; else y = x-1; for (ll i = 1; i <= n; ++i) { if (i == n-x+1) cout << "b"; else if (i == n-y+1) cout << "b"; else cout << "a"; } puts(""); } return 0; }