zoukankan      html  css  js  c++  java
  • Codeforces Round #629 (Div. 3) B. K-th Beautiful String(找规律)

    For the given integer nn (n>2n>2 ) let's write down all the strings of length nn which contain n2n−2 letters 'a' and two letters 'b' in lexicographical (alphabetical) order.

    Recall that the string ss of length nn is lexicographically less than string tt of length nn , if there exists such ii (1in1≤i≤n ), that si<tisi<ti , and for any jj (1j<i1≤j<i ) sj=tjsj=tj . The lexicographic comparison of strings is implemented by the operator < in modern programming languages.

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

    1. aaabb
    2. aabab
    3. aabba
    4. abaab
    5. ababa
    6. abbaa
    7. baaab
    8. baaba
    9. babaa
    10. bbaaa

    It is easy to show that such a list of strings will contain exactly n(n1)2n⋅(n−1)2 strings.

    You are given nn (n>2n>2 ) and kk (1kn(n1)21≤k≤n⋅(n−1)2 ). Print the kk -th string from the list.

    Input

    The input contains one or more test cases.

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

    Each test case is written on the the separate line containing two integers nn and kk (3n105,1kmin(2109,n(n1)2)3≤n≤105,1≤k≤min(2⋅109,n⋅(n−1)2) .

    The sum of values nn over all test cases in the test doesn't exceed 105105 .

    Output

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

    Example
    Input
    Copy
    7
    5 1
    5 2
    5 8
    5 10
    3 1
    3 2
    20 100
    
    Output
    Copy
    aaabb
    aabab
    baaba
    bbaaa
    abb
    bab
    aaaaabaaaaabaaaaaaaa
    因为b只有两个,所以我观察了一下b的出现的位置,发现很有规律。假设最后一位是1,第一位是n,则第一个b出现的位置类似1 22 333 4444......只不过要再偏移一位;第二个出现的b的位置为 1 12 123 1234......这样只需要判断一下k在哪一组里,把相应的位置打上标记即可。由于所有test case的n的和不超过1e5,所以暴力乱搞即可。
    #include <bits/stdc++.h>
    using namespace std;
    bool b[100005]={0};
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            long long n,k;
            memset(b,0,sizeof(b));
            cin>>n>>k;//所有n的和不超过1e5 
            long long i;
            long long nxt;
            for(i=1;i<=n;i++)
            {
                if(i*(i-1)/2<k&&i*(i+1)/2>=k)
                {
                    nxt=i+1;
                    break;
                }
            }
            b[n-nxt+1]=1;
            b[n-(k-(nxt-1)*(nxt-2)/2)+1]=1;
            for(i=1;i<=n;i++)
            {
                if(b[i]==0)printf("a");
                else printf("b");
            }
            cout<<endl;
        }
        return 0;
    }
    //10
    //5 1
    //5 2
    //5 3
    //5 4
    //5 5
    //5 6
    //5 7
    //5 8
    //5 9
    //5 10
  • 相关阅读:
    【模拟+排序】花生采摘 luogu-1086
    【模拟】玩具谜题 luogu-1563
    【并查集模板】并查集模板 luogu-3367
    【字符串+排序】宇宙总统 luogu-1781
    【队列+模拟】机器翻译 luogu-1540
    【Lucas组合数定理+中国剩余定理】Mysterious For-HDU 4373
    【Lucas组合数定理】组合-FZU 2020
    【贪心+排序】营养膳食
    「JSOI2013」贪心的导游
    「JSOI2013」哈利波特和死亡圣器
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12579135.html
Copyright © 2011-2022 走看看