B. K-th Beautiful String

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

  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)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;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值