Codeforces (ccpc-wannafly camp day2) L. Por Costel and the Semipalindromes

题目链接:http://codeforces.com/gym/100923/problem/L

分析:题目要求序列首尾相同,在此基础上的字典序第k个;因为只存在a,b所以我们把它等效成0和1的话,字典序k也就是二进制下的k,将其最后一位与序列头部相同即可

 1 #include<iostream>
 2 #include<sstream>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<string>
 6 #include<cstring>
 7 #include<algorithm>
 8 #include<functional>
 9 #include<iomanip>
10 #include<numeric>
11 #include<cmath>
12 #include<queue>
13 #include<vector>
14 #include<set>
15 #include<cctype>
16 const double PI = acos(-1.0);
17 const int INF = 0x3f3f3f3f;
18 const int NINF = -INF - 1;
19 typedef long long ll;
20 #define MOD 1000000007
21 using namespace std;
22 typedef pair<int, int> P;
23 ll t, n, ans, temp;
24 int main()
25 {
26     freopen("semipal.in","r",stdin);
27     freopen("semipal.out","w",stdout);
28     ll t;
29     cin >> t;
30     while (t--)
31     {
32         ll n, k;
33         cin >> n >> k;
34         char s[1005];
35         k--;
36         for (int i = n - 2; i >= 0; i--)
37         {
38             if (k % 2) s[i] = 'b';
39             else s[i] = 'a';
40             k /= 2;
41         }
42         s[n-1] = s[0];
43         for (int i = 0; i < n; ++i)
44             cout << s[i];
45         cout << endl;
46     }
47     return 0;
48 }

猜你喜欢

转载自www.cnblogs.com/veasky/p/11229339.html
今日推荐