k.aaaaaaaaaaA heH heH nuN
题目描述:
Vasily Tadokorov is a stringologist. He thinks a string is fragrant if it can be divided into two parts: nunhehheh as the prefix and a number of (excluding 0) a as the suffix. For example, nunhehhehaaaaaa is fragrant, but nunhehheh and nunhehhehoooaaa are not fragrant.
Today Vasily Tadokorov has some strings consisting of lowercase English letters. For each string, he wants to know how many subsequences of this string are fragrant. A string aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (including 0) characters.
The above is a problem of string that Vasily came up with. As we know, a problem usually has several examples for better understanding. However, Vasily gets buried in making some fragrant examples. After 2000 years, he finally makes two perfect examples as follows.
Input
The first line contains an integer TT (1≤T≤1000), denoting the number of tasks.
Each of the next TT lines contains an integer nn (0≤n≤109).
Output
For each test case, output one string consisting of only lowercase English letters in a single line indicating the answer. You need to ensure that the sum of the length over all the output strings does not exceed 106106. It can be proved that a solution always exists. If there are multiple solutions, print any.
题意:
给出多组例,每一组给出一个数字,让我们构造一个字符串,让其组合数能达到给出的数字
思路:
1.我们首先可以发现,当前缀已经是nunhehhe的时候(nunhehhe + h+a个x),在一个h后面a个x能构成2^x-1组
2.当 nunhehhe hhhha的时候 ,有p个h,则有p个有效子序列
这时候我们结合上面的(1)2^x-1个我们可以想到的是能不能构成2^x能,以便使用二进制,所以结合(2),我们在最后一个a前加一个h,这不就构成了2^x了?所以,我们根据二进制来确定h的位置,首先输出nunhehhe、再在后面加上32个a
接着我们根据二进制中的1来确定h
从右往左的a和a之间的空隙就是题目所给的数字的二进制位(从第0位开始),只需要对原数字二进制位数上为1的对应位置插入一个h,在再最后一个可以插入的位置插入h,就可以为答案贡献2^x.
看完来个赞吧!
AC代码:
#include<bits/stdc++.h>
using namespace std;
int cnt[42];
typedef long long ll;
ll pow2[40];
void init()
{
pow2[0]=1;
for(int i=1;i<=31;i++)
pow2[i]=pow2[i-1]*2;
return;
}
int main(){
init();
int t;
cin>>t;
while(t--){
int n,ans=0;
cin>>n;
for(int i=31;i>0;i--)
cnt[i]=0;
for(int i=31;i>0;i--)
{
if(n>=pow2[i])
{
cnt[i]++;
n-=(pow2[i]);
cnt[1]++;
}
}
if(n%2)
cnt[1]++;
cout<<"nunhehhe";
for(int i=31;i>0;i--)
{
for(int j=0;j<cnt[i];j++)
cout<<"h";
cout<<"a";
}
cout<<"\n";
}
}