CF1328B.K-th Beautiful String

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

For the given integer nn (n>2n \gt 2) let's write down all the strings of length nn which contain n−2n-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 (1≤i≤n1 \le i \le n), that si<tis_i \lt t_i, and for any jj (1≤j<i1 \le j \lt 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⋅(n−1)2\frac{n \cdot (n-1)}{2} strings.

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

对于给定的整数 nn(n>2n \gt 2),请写出所有长度为 nn 的字符串,这些字符串恰好包含 n−2n-2 个字母 'a' 和 2 个字母 'b',并按字典序(字母顺序)排列。

回忆一下:长度为 nn 的字符串 ss 在字典序上小于长度为 nn 的字符串 tt,当且仅当存在某个下标 ii(1≤i≤n1 \le i \le n),使得 si<tis_i \lt t_i,且对任意 jj(1≤j<i1 \le j \lt i)均有 sj=tjs_j = t_j。现代编程语言中,字符串的字典序比较由运算符 < 实现。

例如,当 n=5n=5 时,这些字符串如下(顺序至关重要):

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

容易证明,这样的字符串列表恰好包含 n⋅(n−1)2\frac{n \cdot (n-1)}{2} 个字符串。

现给你 nn(n>2n \gt 2)和 kk(1≤k≤n⋅(n−1)21 \le k \le \frac{n \cdot (n-1)}{2})。请输出该列表中第 kk 个字符串。

输入格式

The input contains one or more test cases.

The first line contains one integer tt (1≤t≤1041 \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 (3≤n≤105,1≤k≤min⁡(2⋅109,n⋅(n−1)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.

输入包含一个或多个测试用例。

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试中测试用例的数量。随后是 tt 个测试用例。

每个测试用例独占一行,包含两个整数 nn 和 kk(3≤n≤1053 \le n \le 10^5,1≤k≤min⁡(2⋅109,n⋅(n−1)2)1 \le k \le \min(2\cdot10^9, \frac{n \cdot (n-1)}{2}))。

测试中所有测试用例的 nn 值之和不超过 10510^5。

输出格式

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).

对于每个测试用例,输出上述所有长度为 nn 的字符串按字典序(字母序)排序后的列表中第 kk 个字符串。

输入输出样例

  • 输入#1

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

    输出#1

    aaabb
    aabab
    baaba
    bbaaa
    abb
    bab
    aaaaabaaaaabaaaaaaaa

输入解题思路,AI测评打分。不知道怎么写?

首页