semipal.in / semipal.out

Por Costel the pig, our programmer in-training, has recently returned from the Petrozaporksk training camp. There, he learned a lot of things: how to boil a cob, how to scratch his belly using his keyboard, etc... He almost remembers a programming problem too:

A semipalindrome is a word  for which there exists a subword  such that  is a prefix of  and  (reverse ) is a suffix of . For example, 'ababba' is a semipalindrom because the subword 'ab' is prefix of 'ababba' and 'ba' is suffix of 'ababba'.

Let's consider only semipalindromes that contain letters 'a' and 'b'. You have to find the -th lexicographical semipalindrome of length .

Por Costel doesn't remember if the statement was exactly like this at Petrozaporksk, but he finds this problem interesting enough and needs your help to solve it.

Input

On the first line of the file semipal.in, there is an integer  () representing the number of test cases. On the next  lines there are 2 numbers,  ( and K  where  is the number of semipalindromes of length .

Output

In the output file semipal.out, there should be  lines, the -th of which should contain the answer for the -th test.

Example

Input
2
5 1
5 14
Output
aaaaa
bbabb

显然只需要保证开头和结尾字母相同,就一定是合法的啦。

然后就把中间的部分得到即可。

#include<cstdio>
using namespace std;
typedef long long ll;
int T,n;
ll m;
char a[70];
int main()
{
// freopen("l.in","r",stdin);
freopen("semipal.in","r",stdin);
freopen("semipal.out","w",stdout);
scanf("%d",&T);
for(;T;--T)
{
scanf("%d%I64d",&n,&m);
// ll all=1ll<<(n-1);
bool flag=0;
if(m>(1ll<<(n-2)))
{
m-=(1ll<<(n-2));
flag=1;
}
--m;
for(int i=1;i<=n-2;++i)
{
a[i]=(m%2 ? 'b' : 'a');
m/=2;
}
if(flag)
{
putchar('b');
for(int i=n-2;i>=1;--i)
putchar(a[i]);
puts("b");
}
else
{
putchar('a');
for(int i=n-2;i>=1;--i)
putchar(a[i]);
puts("a");
}
}
return 0;
}

【找规律】Gym - 100923L - Por Costel and the Semipalindromes的更多相关文章

  1. 【Heap-dijkstra】Gym - 100923B - Por Costel and the Algorithm

    algoritm.in / algoritm.out Even though he isn't a student of computer science, Por Costel the pig ha ...

  2. 【分块打表】Gym - 100923K - Por Costel and the Firecracker

    semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...

  3. 【数形结合】Gym - 100923I - Por Costel and the Pairs

    perechi3.in / perechi3.out We don't know how Por Costel the pig arrived at FMI's dance party. All we ...

  4. 【并查集】Gym - 100923H - Por Costel and the Match

    meciul.in / meciul.out Oberyn Martell and Gregor Clegane are dueling in a trial by combat. The fight ...

  5. 【动态规划】Gym - 100923A - Por Costel and Azerah

    azerah.in / azerah.out Por Costel the Pig has received a royal invitation to the palace of the Egg-E ...

  6. 【带权并查集】Gym - 100923H - Por Costel and the Match

    裸题. 看之前的模版讲解吧,这里不再赘述了. #include<cstdio> #include<cstring> using namespace std; int fa[10 ...

  7. Codeforces (ccpc-wannafly camp day2) L. Por Costel and the Semipalindromes

    题目链接:http://codeforces.com/gym/100923/problem/L 分析:题目要求序列首尾相同,在此基础上的字典序第k个:因为只存在a,b所以我们把它等效成0和1的话,字典 ...

  8. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

  9. codeforces Gym 100418D BOPC 打表找规律,求逆元

    BOPCTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

随机推荐

  1. MySQL的字符集小结

    正确了解MySQL的字符集问题,能够从根本上解决乱码的困扰. 首先,MySQL的字符集问题主要是两个概念,一个是Character Sets,一个是Collations,前者是字符内容及编码,后者是对 ...

  2. Socket和ServerSocket学习笔记

    对于即时类应用或者即时类的游戏,HTTP协议很多时候无法满足于我们的需求.这会,Socket对于我们来说就非常实用了.下面是本次学习的笔记.主要分异常类型.交互原理.Socket.ServerSock ...

  3. 从一段字符串中去除数字的shell方法

  4. IDEA中如何使用svn

    1.安装tortoiseSVN客户端时,这里一定要勾选上,否则使用时会报错. 2.安装好之后,想启用idea的SVN插件还需要在idea配置一下,file - setting 按钮打开设置界面 或者( ...

  5. 51nod数字1的数量

    这道题瞎jbyy了很久 方法可能很奇怪... #include<cstdio> #include<cstring> #include<algorithm> #inc ...

  6. 【STSRM12】夏令营

    [题意]n个数划分成k段,每段的价值为段内不同数字的数量,求最大总价值 [算法]DP+线段树 [题解] f[i][j]表示前i个数字划分成j段的最大价值. f[i][j]=max(f[k][j-1]+ ...

  7. 【洛谷 P4289】[HAOI2008]移动玩具(搜索)

    其实这题可以不用状压.. 提供一种新思路. 我们在读入目标棋盘的时候,把当前位置的数和当前棋盘进行比较,如果不一样,如果当前是\(1\),目标是\(0\),那么我们就把当前位置加入\(needmove ...

  8. bzoj 1083 最小生成树

    裸的最小生成树. /************************************************************** Problem: User: BLADEVIL Lan ...

  9. how to remove an element in lxml

    import lxml.etree as et xml=""" <groceries> <fruit state="rotten"& ...

  10. TCP/IP Http的区别

    TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据. 关于TCP/IP和HTTP协议的关系,网络有一段比较容易理解的介绍:“我们在传输数据时,可以 ...