CF BUDDY
← Problems·

1328B · K-th Beautiful String

1300 · binary search, brute force, combinatorics

Problem: For the given integer nn (n>2n > 2) let's write down all the strings of length nn which contain n2n-2 letters 'a' and two letters 'b' in lexicographical (alphabetical) order.

Recall that the string ss of length nn is lexicographically less than string tt of length nn, if there exists such ii (1in1 \le i \le n), that si<tis_i < t_i, and for any jj (1j<i1 \le j < i) sj=tjs_j = t_j. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.

For example, if n=5n=5 the strings are (the order does matter):

  1. aaabb
  2. aabab
  3. aabba
  4. abaab
  5. ababa
  6. abbaa
  7. baaab
  8. baaba
  9. babaa
  10. bbaaa

It is easy to show that such a list of strings will contain exactly n(n1)2\frac{n \cdot (n-1)}{2} strings.

You are given nn (n>2n > 2) and kk (1kn(n1)21 \le k \le \frac{n \cdot (n-1)}{2}). Print the kk-th string from the list.

Input Format: The input contains one or more test cases.

The first line contains one integer tt (1t1041 \le t \le 10^4) — the number of test cases in the test. Then tt test cases follow.

Each test case is written on the the separate line containing two integers nn and kk (3n105,1kmin(2109,n(n1)2)3 \le n \le 10^5, 1 \le k \le \min(2\cdot10^9, \frac{n \cdot (n-1)}{2}).

The sum of values nn over all test cases in the test doesn't exceed 10510^5.

Output Format: For each test case print the kk-th string from the list of all described above strings of length nn. Strings in the list are sorted lexicographically (alphabetically).

Sample Cases

Case 1

Input

7
5 1
5 2
5 8
5 10
3 1
3 2
20 100

Output

aaabb
aabab
baaba
bbaaa
abb
bab
aaaaabaaaaabaaaaaaaa

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback