1.题目大意:给出一个只包含两个’
b
b
b‘且剩余字符全为’
a
a
a'的字符串,求构成的所有字符串中第
k
k
k小的字符串
2.对于第 k k k小,我们难以下手,但是反过来想,求第 k k k大不是很好求吗,那么先初始化所有的子串,找出求第 k k k大的规律,再倒着求第 k k k小即可。规律显然是第一个’ b b b‘的位置,由 1 − n − 1 1-n-1 1−n−1,当第一个’ b b b‘在下标 1 1 1时,第二个’ b b b‘由大到小有四种方法。得出只要确定第 1 1 1个’ b b b‘的位置i就能得出第二个’ b b b'有 n − i n-i n−i种位置,累积的就是第 k k k大,先初始化保存到数组中,最后倒着查询第 k k k小,就能得出两个 b b b的位置,就能构造出字符串了
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x7fffffff;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=3e5+10;
int f[maxn];
int n,t,k;
void init(){
for(int i=1;i<=n;i++){
f[i]=n-i;
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
cin>>n>>k;
init();
int ans=0,p=n-1;
while(ans+f[p]<k){
ans+=f[p--];
}
int x=k-ans;
int y=n+1-x;
for(int i=1;i<=n;i++){
if(i==p || i==y) cout<<'b';
else cout<<'a';
}
cout<<endl;
}
return 0;
}