B.K-th Beautiful String

找规律

可以发现第一个b的位置是x,第二个b位置在最后一个时,它所在的字典序为$x*(x-1)/2$

于是解方程$x*(x-1)/2=n$

#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
int main()
{
    int t; cin >> t;
    while (t--)
    {
        ll n;
        ll k; cin >> n >> k;
        ll x = (1 + sqrt(1 + 8 * k)) / 2;
        ll y = k - x * (x - 1) / 2;
        if (y) ++x;
        else y = x-1;
        for (ll i = 1; i <= n; ++i)
        {
            if (i == n-x+1) cout << "b";
            else if (i == n-y+1) cout << "b";
            else cout << "a";
        }
        puts("");
    }
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/xiongyuqing/p/12578948.html
今日推荐