与えられた整数n(n> 2)について、辞書式(アルファベット順)でn-2文字の「a」と2文字の「b」を含む長さnのすべての文字列を書き留めましょう。
長さnの文字列sは、辞書式順序で長さnの文字列tよりも小さいことを思い出してください。そのようなi(1≤i≤n)、si <tiが存在する場合、および任意のj(1≤j<i)sj = tj 。文字列の辞書式比較は、最新のプログラミング言語では演算子<によって実装されます。
たとえば、n = 5の場合、文字列は次のようになります(順序は重要です)。
aaabb
aabab
aabba
abaab
アベバ
abbaa
baaab
baaba
babaa
bbaaa
それは文字列のようなリストは、(N-1)正確にn⋅2つの文字列が含まれていることを示すのは容易です。
n(n> 2)とk(1≤k≤n・(n-1)2)が与えられます。リストからk番目の文字列を出力します。
入力
入力には、1つ以上のテストケースが含まれます。
最初の行には、1つの整数t(1≤t≤104)—テスト内のテストケースの数が含まれています。次に、t個のテストケースが続きます。
各テストケースは、2つの整数nとk(3≤n≤105,1≤k≤min(2・109、n・(n-1)2)を含む別々の行に記述されています。
テストのすべてのテストケースの値nの合計は105を超えません。
出力
各テストケースについて、長さnの上記のすべての文字列のリストからk番目の文字列を出力します。リスト内の文字列は、辞書式(アルファベット順)に並べ替えられます。
Example.comは
中入力
7
5 1
2 5
5 8
10 5
1 3
2 3
〜100 20の
出力さ
aaabb
aabab
Baaba
bbaaa
完全半分ABBの
バブ
aaaaabaaaaabaaaaaaaa
質問:
長さnの文字列はn-2aと2bで構成され、各配置は辞書式順序で配置されます。長さnとシーケンス番号kを入力し、k番目のグループシーケンス配置状況の文字列を出力します。
そして
解決策:
最初に接頭辞付きの前処理と記録があり、各位置が最後から2番目の位置に基づいている場合b。次に、トラバースしてbの場所を見つけます。
ACコード:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define debug() puts("what the fuck!")
#define debug(a) cout<<#a<<"="<<a<<endl;
#define speed {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
#define ll long long
#define mod 998244353
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const double esp_0 = 1e-6;
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll x, ll y) {
return x * y / gcd(x, y);
}
ll extends_gcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll gcdd=extends_gcd(b, a % b, x, y);
ll temp = x;
x = y;
y = temp - (a / b) * y;
return gcdd;
}
int pos[maxn], sum[maxn];
int main(){
speed;
mem(pos, 0);
mem(sum, 0);
for (int i = 2; i <= maxn - 1; i++)pos[i] = pos[i - 1] + 1, sum[i] = sum[i - 1] + pos[i];
//for (int i = 1; i <= maxn - 1; ++i) {
// cout << i << ": " << pos[i] << " " << sum[i] << endl;
//}
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
int step = 0;
for (int i = 1; i <= n; ++i) {
if (k <= sum[i]) {
step = i;
break;
}
}
k -= sum[step - 1];
for (int i = 0; i < n; ++i) {
if (i == n - step || i == n - k)cout << 'b';
else cout << 'a';
}
cout << endl;
}
return 0;
}