码迷,mamicode.com
首页 > 其他好文 > 详细

【找规律】Gym - 100923L - Por Costel and the Semipalindromes

时间:2017-01-20 07:35:51      阅读:430      评论:0      收藏:0      [点我收藏+]

标签:class   nbsp   learn   represent   bsp   分享   stdout   where   integer   

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

标签:class   nbsp   learn   represent   bsp   分享   stdout   where   integer   

原文地址:http://www.cnblogs.com/autsky-jadek/p/6321749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!