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
题意:
让你求长度为n 头尾相同 第k小子序列
解; a看为0 b=1
就是从0数到k-1的二进制
#include<bits/stdc++.h>
using namespace std;
#define ll long long
/*
ll solve(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)
ans*=a;
a*=a;
b>>=1;
}
return ans;
}
int main()
{
// freopen("azerah.in","r",stdin);
freopen("azerah.out","w",stdout);
ll t,i,j;
cin>>t;
{
while(t--)
{
ll n,a,s1=0,s2=0;
scanf("%lld",&n);
for(i=0;i<n;i++)
{
scanf("%lld",&a);
if(a&1) s1++;
else s2++;
}
if(!s1&&s2)
cout<<solve(2,s2)-1<<endl;
else if(s1&&!s2)
cout<<solve(2,s1-1)-1<<endl;
else if(s1&&s2)
cout<<solve(2,s1/2+s2)-1<<endl;
}
}
return 0;
}*/
int main()
{
freopen("semipal.in","r",stdin);
freopen("semipal.out","w",stdout);
ll t,i,j;
while(cin>>t)
{
while(t--)
{
ll n,k;
scanf("%lld%lld",&n,&k);
char s[1111];
k--;
for(i=n-2;i>=0;i--)
{
if(k%2) s[i]='b';
else s[i]='a';
k/=2;
}
s[n-1]=s[0];
for(i=0;i<n;i++)
printf("%c",s[i]);
puts("");
}
}
return 0;
}