• CodeForces 1328B


    For the given integer n (n>2) let’s write down all the strings of length n which contain n−2 letters ‘a’ 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≤1e4) — 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≤1e5,1≤k≤min(2⋅1e9,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

    题目大意:
    输入一个t表示有t组测试样例,对于每组测试样例,给出n和k,表示有n长度的ab串,由n-2个a和2个b组成,k表示输出第k个字典序(可以看一下题面的样例)。

    解题思路:
    这道题n的范围是1e5,我刚开始用的next_permutation()函数,TLE了一发,发现这个是一道思维题,我们可以根据n和k的值推算出 b 的位置,然后把对应的位置替换成b即可。我们可以根据给出的样例得到规律,第一个b在倒数第二个位置的有1个,倒数第三个b位置的有3个,倒数第四个b有4个,我们可以得到第一个b的位置数列是 1 3 6 10… 这样,推一个公式:n(n-1)/2,n=2时倒数第二个b截止到第1个,n=3时倒数第三个b截止到第3个,就能得到 n的数即是倒数第n个b的截止数。这样枚举到1e5就能确定第一个b的位置了。再看k:k也有公式,n-(n(n-1)/2-k)-1,不过要先根据前面的公式推出n所处的区间,比如n如果等于2,则说明第一个b应该在倒数第二个位置,之后把n=2带进去,就等得到第二个b的位置。ps:求出来的值是表示倒数第几个位置。AC代码:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int _max=1e5+50;
    using ll = long long;
    int main()
    {
    	int t;
    	cin>>t;
    	while(t--)
    	{
    		ll n,k;
    		cin>>n>>k;
    		ll p,q;
    		for(ll i=1;i<_max;i++)
    		  if(k<=(i*(i-1)/2))
    		  {
    			  p=i;//第一个b
    			  break;
    		  }
    		q=p-(p*(p-1)/2-k)-1;//第二个b
    		//cout<<p<<" "<<q<<endl;
    		string s(n,'a');
    		s[n-p]=s[n-q]='b';
    		cout<<s<<endl;
    	}
    	//system("pause");
    	return 0;
    }
    
  • 相关阅读:
    javascript:getElementsByName td name
    C# 批量复制文件
    笨笨图片批量下载器[C# | WinForm | 正则表达式 | HttpWebRequest]
    浩方魔兽"去"小喇叭终极解决方案[Warcraft III]
    JavaScript——DataListBox(组合框)
    PowerDesigner 12 根据名称生成注释(完整示例)
    笨笨图片批量抓取下载 V0.2 beta[C# | WinForm | 正则表达式 | HttpWebRequest | Async异步编程]
    使用IHttpHandler做权限控制[ASP.NET | IHttpHandler | AjaxPro | UserHostName]
    AjaxPro 未定义错误
    北京有趣地名(二)
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294275.html
Copyright © 2020-2023  润新知