For the given integer n (n>2) let’s write down all the strings of length n which contain n−2 letters ‘a’ and two letters ‘b’ in lexicographical (alphabetical) order.

Recall that the string s of length n is lexicographically less than string t of length n, if there exists such i (1≤i≤n), that si<ti, and for any j (1≤j<i) sj=tj. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.

For example, if n=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)2 strings.

You are given n (n>2) and k (1≤k≤n⋅(n−1)2). Print the k-th string from the list.

Input
The input contains one or more test cases.

The first line contains one integer t (1≤t≤104) — the number of test cases in the test. Then t test cases follow.

Each test case is written on the the separate line containing two integers n and k (3≤n≤105,1≤k≤min(2⋅109,n⋅(n−1)2).

The sum of values n over all test cases in the test doesn’t exceed 105.

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

Example
Input
7
5 1
5 2
5 8
5 10
3 1
3 2
20 100
Output
aaabb
aabab
baaba
bbaaa
abb
bab
aaaaabaaaaabaaaaaaaa
思路:考虑两个b的位置,左边的b右边有几个位置,对于这个b来说,就有几种情况,二分去找这个区间,然后再判断第二个b的位置就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;int n;ll k;int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%lld",&n,&k);ll l=1,r=n-1,mid;while(l<=r){int mid=l+r>>1;if((1ll+mid)*mid/2<=k) l=mid+1;else r=mid-1;}ll pos1,pos2;if((r+1)*r/2==k){pos1=n-r;pos2=pos1+1;} else{pos1=n-l;pos2=n-(k-(r+1)*r/2-1);}for(int i=1;i<=n;i++) {if(i==pos1||i==pos2) cout<<'b';else cout<<'a';}cout<<endl;}return 0;
}

努力加油a啊,(o)/~

K-th Beautiful String CodeForces - 1328B(二分+数学)相关推荐

  1. Long Beautiful Integer CodeForces - 1268A(贪心构造)

    You are given an integer x of n digits a1,a2,-,an, which make up its decimal notation in order from ...

  2. K - Anton and Lines CodeForces - 593B

    K - Anton and Lines CodeForces - 593B 思路 求出两个边界处,每条线对应的 y,如果出现(y1i - y1j)* (y2i - y2j) < 0, 则相交 复 ...

  3. 微软校招编程题Beautiful String的状态机解法

    昨天碰巧看到一道微软校招的编程题,题目大意如下: 如果一个字符串包括三组或者更多组的连续升序字母,每组长度相等,那么我们就称这个字符串是Beautiful String如下是一些Beautiful S ...

  4. 建立一个Map实例,k值为String类型,v值为Integer类型。依次用put方法输入如下: map.put(Kobe, 24); map.put(James, 6); map.put(

    建立一个Map实例,k值为String类型,v值为Integer类型.依次用put方法输入如下: map.put("Kobe", 24); map.put("James& ...

  5. CodeForces - 1560F2 Nearest Beautiful Number (hard version)(二分+数位dp)

    题目链接:点击查看 题目大意:给出一个十进制数字 nnn 和一个约束 kkk,问大于等于 nnn 且满足不同的数位个数小于等于 kkk 的最小的数字是多少 题目分析:自己写的贪心太丑了,就不放上来丢人 ...

  6. Codeforces Round #127 (Div. 1) E. Thoroughly Bureaucratic Organization 二分 数学

    E. Thoroughly Bureaucratic Organization 题目连接: http://www.codeforces.com/contest/201/problem/E Descri ...

  7. Success Rate CodeForces - 807C (数学+二分)

    You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...

  8. Codeforces 1359 C. Mixing Water (二分 /数学)

    链接 C. Mixing Water 题意 : 给一个容器,往里面不断地倒 热水 冷水 交替进行,问最少倒多少杯水后容器内的平均温度最接近于 T. 思路: 首先很容易观察到,如果倒进去的水是偶数杯,那 ...

  9. HDU - 5030 Rabbit's String(后缀数组+二分)

    题目链接:点击查看 题目大意:给出一个字符串,现在要求将其分为不大于k个连续的子串,对于每个子串求出字典序最大的子串,现在要求所有子串的最大子串的最大值最小,输出这个最大子串 题目分析:最大值最小,标 ...

最新文章

  1. HDOJ 1233 还是畅通工程
  2. JSP+servlet生成验证码并验证
  3. linux(八)__yum工具
  4. 阿里云物联网平台,三要素生成hmacmd5,hmacsha1和hmacsha256,password算法+hashmd5,hashsha1,hashsha256算法
  5. MySQL 快速定位性能问题
  6. JAVA入门级教学之(你是否理解HelloWorld的这段经典的代码的注释)
  7. 删除Windows 文本右键选项
  8. php 中PHP_EOL使用
  9. CakePHP:链接地址问题(不用mod_rewrite,IIS)
  10. Windows as a Service(3)——使用SCCM管理Windows10更新
  11. HDFS简介及其功能
  12. 一切从恢复系统开始(尽量不要学我恢复数据库的方法,我是个懒人^_^)
  13. C语言员工信息管理系统
  14. 各种网络协议的类型、优缺点、作用
  15. (轉貼) 馬英九特別費案起訴書全文 (News)
  16. UnReal与Unity游戏引擎对比分析
  17. 2010年Ei收录的中国期刊
  18. python安装cpickle_python中cPickle
  19. USB OTG功能是什么意思?
  20. rm: cannot remove `/usr/local/tmp/‘: Directory not empty

热门文章

  1. C,C++,C#note
  2. mysql pdo 读取字段名_PHP使用PDO从mysql读取大量数据处理详解
  3. 禁止ensp弹出提示
  4. php stdclass,php中new stdclass()用在什么场景
  5. session共享mysql_多服务器session共享之mysql共享
  6. 2. with check option能起什么作用?_家装要选择第三方装修监理吗?为什么?
  7. virtualbox php mac,mac一体机通过Oracle VM VirtualBox装win8.1系统
  8. sqlserver循环like变量_numba从入门到精通(6)—numba与循环与并行
  9. ensp保存文件在哪_eNSP——配置通过FTP进行文件操作
  10. 浅析Hibernate映射(一)——基本映射