結果

問題 No.2893 Minahoshi (Hard)
ユーザー Shaojia
提出日時 2024-09-17 11:50:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,751 bytes
コンパイル時間 2,729 ms
コンパイル使用メモリ 216,572 KB
最終ジャッジ日時 2025-02-24 09:05:39
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define rep(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii<=Ii##_;Ii++)
#define per(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii>=Ii##_;Ii--)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned uint;
typedef long double db;
#define fir first
#define sec second
#define siz(Aa) ((int)(Aa).size())
#define all(Aa) (Aa).begin(),(Aa).end()
#define ckmx(Aa,Bb) (Aa=max(Aa,Bb))
#define ckmn(Aa,Bb) (Aa=min(Aa,Bb))

const int N=1<<18;
int n,k,s[N],st,root,lst;
bool vis[N],to[N],out[N];
void dfs(int x){
	for(int y:{x<<1,x<<1|1}){
		if(!vis[y]){
			vis[y]=true;
			dfs(y&((1<<k-1)-1));
			s[st++]=y;
		}
	}
}
signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);
	int T;cin>>T;
	while(T--){
		cin>>n;
		if(n==1){
			cout<<"a";
			goto hell;
		}
		k=1;
		while((1<<k)+k-1<=n) k++;
		k--;
		fill(vis,vis+(1<<k),false);
		st=0;
		dfs(0);
		reverse(s,s+st);
		assert(st==(1<<k));
		rep(i,0,st-2) to[s[i]]=s[i+1]&1;
		to[s[st-1]]=s[0]&1;
		fill(vis,vis+(1<<k),false);
		lst=n-(k-1)-(1<<k);
		root=-1;
		fill(out,out+(1<<k),false);
		rep(i,0,(1<<k)-1) if(!vis[i]){
			out[i]=true;
			for(int j=i,jj;;j=jj){
				vis[j]=true;
				lst--;
				jj=(j<<1|!to[j])&(1<<k)-1;
				if(jj==i) break;
			}
			if(lst<=0){
				root=(i<<1|to[i])&(1<<k)-1;
				break;
			}
		}
		assert(root!=-1);
		per(i,k-1,0) cout<<char((root>>i&1)+'a');
		lst=n-k;
		while(true){
			if(out[root]){
				for(int j=root,jj;;j=jj){
					if(!lst) goto hell;
					lst--;
					cout<<char(!to[j]+'a');
					jj=(j<<1|!to[j])&(1<<k)-1;
					if(jj==root) break;
				}
			}
			if(!lst) goto hell;
			lst--;
			cout<<char(to[root]+'a');
			root=(root<<1|to[root])&(1<<k)-1;
		}
		hell:;
		cout<<"\n";
	}
return 0;}
/*
*/
0