time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
For the given integer nn (n>2n>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≤i≤n), that si<tisi<ti, and for any jj (1≤j<i1≤j<i) sj=tjsj=tj. 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):
- aaabb
- aabab
- aabba
- abaab
- ababa
- abbaa
- baaab
- baaba
- babaa
- bbaaa
It is easy to show that such a list of strings will contain exactly n⋅(n−1)2n⋅(n−1)2 strings.
You are given nn (n>2n>2) and kk (1≤k≤n⋅(n−1)21≤k≤n⋅(n−1)2). Print the kk-th string from the list.
Input
The input contains one or more test cases.
The first line contains one integer tt (1≤t≤1041≤t≤104) — 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≤n≤105,1≤k≤min(2⋅109,n⋅(n−1)2).
The sum of values nn over all test cases in the test doesn't exceed 105105.
Output
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).
Example
input
Copy
7 5 1 5 2 5 8 5 10 3 1 3 2 20 100
output
Copy
aaabb aabab baaba bbaaa abb bab aaaaabaaaaabaaaaaaaa
解题说明:此题是一道数学题,通过找规律,第一个 b在倒数第二位有 1个,倒数第三位 2个,倒数第四位三个,第二个 b 的位置就是 k−1−2−3−..剩下的数字倒着数,然后倒着输出即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int t, ii, n, k, sum, i, j;
scanf("%d", &t);
for (ii = 0; ii<t; ii++)
{
scanf("%d %d", &n, &k);
sum = 0;
k--;
for (i = 0;; i++)
{
if (sum + i + 1 > k)
{
break;
}
sum += i + 1;
}
k -= sum;
i++;
for (j = 0; j<n; j++)
{
if (j == n - i - 1 || j == n - k - 1)
{
printf("b");
}
else
{
printf("a");
}
}
printf("\n");
}
return 0;
}