日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

K-th Beautiful String CodeForces - 1328B(二分+数学)

發(fā)布時(shí)間:2023/12/15 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 K-th Beautiful String CodeForces - 1328B(二分+数学) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

For the given integer n (n>2) let’s write down all the strings of length n which contain n?2 letters ‘a(chǎn)’ and two letters ‘b’ in lexicographical (alphabetical) order.

Recall that the string s of length n is lexicographically less than string t of length n, if there exists such i (1≤i≤n), that si<ti, and for any j (1≤j<i) sj=tj. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.

For example, if n=5 the strings are (the order does matter):

aaabb
aabab
aabba
abaab
ababa
abbaa
baaab
baaba
babaa
bbaaa
It is easy to show that such a list of strings will contain exactly n?(n?1)2 strings.

You are given n (n>2) and k (1≤k≤n?(n?1)2). Print the k-th string from the list.

Input
The input contains one or more test cases.

The first line contains one integer t (1≤t≤104) — the number of test cases in the test. Then t test cases follow.

Each test case is written on the the separate line containing two integers n and k (3≤n≤105,1≤k≤min(2?109,n?(n?1)2).

The sum of values n over all test cases in the test doesn’t exceed 105.

Output
For each test case print the k-th string from the list of all described above strings of length n. Strings in the list are sorted lexicographically (alphabetically).

Example
Input
7
5 1
5 2
5 8
5 10
3 1
3 2
20 100
Output
aaabb
aabab
baaba
bbaaa
abb
bab
aaaaabaaaaabaaaaaaaa
思路:考慮兩個(gè)b的位置,左邊的b右邊有幾個(gè)位置,對(duì)于這個(gè)b來(lái)說(shuō),就有幾種情況,二分去找這個(gè)區(qū)間,然后再判斷第二個(gè)b的位置就可以了。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;int n;ll k;int main() {int t;scanf("%d",&t);while(t--){scanf("%d%lld",&n,&k);ll l=1,r=n-1,mid;while(l<=r){int mid=l+r>>1;if((1ll+mid)*mid/2<=k) l=mid+1;else r=mid-1;}ll pos1,pos2;if((r+1)*r/2==k){pos1=n-r;pos2=pos1+1;} else{pos1=n-l;pos2=n-(k-(r+1)*r/2-1);}for(int i=1;i<=n;i++) {if(i==pos1||i==pos2) cout<<'b';else cout<<'a';}cout<<endl;}return 0; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的K-th Beautiful String CodeForces - 1328B(二分+数学)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。