Codeforces Round #629 (Div. 3) B. K-th Beautiful String

Camille ·
更新时间:2024-09-20
· 775 次阅读

B. K-th Beautiful String

题目链接-B. K-th Beautiful String
在这里插入图片描述
在这里插入图片描述
题目大意
长度为n的字符串包含n−2n−2n−2个aaa和222个bbb,求按照字典序排列输出第kkk个字符串

解题思路

第一个bbb在倒数第二位有1个字符串,在倒数第三位有2个字符串…在倒数第nnn位时有n−1n-1n−1个字符串 可以根据第一个bbb的位置对字符串进行分组,然后找到第kkk个字符串在第几组里即可,然后再推出第二个bbb的位置即可 记得我们刚开始找的bbb的位置是倒数的,假设bbb在倒数第i位上,那么b就在整数第n+1−in+1-in+1−i位上 具体操作见代码

附上代码

#include #define int long long #define lowbit(x) (x &(-x)) #define endl '\n' using namespace std; const int INF=0x3f3f3f3f; const int dir[4][2]={-1,0,1,0,0,-1,0,1}; const double PI=acos(-1.0); const double eps=1e-10; const int M=1e9+7; const int N=1e5+5; typedef long long ll; typedef pair PII; signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int t; cin>>t; while(t--){ int n,k; cin>>n>>k; int x=0,tmp; for(int i=1;i=k){ x=i-1; break; } } int y=k-x*(x-1)/2-1; x=n-x; y=n-y; for(int i=1;i<=n;i++){ if(i==x||i==y) cout<<"b"; else cout<<"a"; } cout<<endl; } return 0; }
作者:Fiveneves



CodeForces round div string

需要 登录 后方可回复, 如果你还没有账号请 注册新账号