#include<bits/stdc++.h>
using namespace std;
int longest_palindrom(string s){
int output=1, i, l, r, n=s.size();
for(i=0; i<n; ++i){
l = i-1;
r = i+1;
while(l>=0 && r<n && s[l]==s[r]){
l--;
r++;
}
output = max(output, (i-l)*2-1);
l = i;
r = i+1;
while(l>=0 && r<n && s[l]==s[r]){
l--;
r++;
}
output = max(output, (i-l)*2);
}
return output;
}
string solve(int n, int k){
int x = 1, a=1;
for(int i=0; i<n; ++i){ x*=2; a+=x;}
x = a;
vector<string> s(x, "");
int i;
for(i=1; i<x; ++i){
if(i%2!=0) s[i] = s[i/2]+'A';
else s[i] = s[(i-1)/2]+'P';
if((int)s[i].size() == n && longest_palindrom(s[i])==k) return s[i];
}
return "NIE";
}
string abc(int n){
string curr = "P";
int x = 1, i;
while(x<n){
for(i=0; i<x; ++i){
if(curr[i]=='P') curr+="A";
else curr+="P";
}
x*=2;
}
curr.resize(n);
return curr;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin.tie(0);
int n, k, i, t;
cin >> t;
for(int j=0; j<t; ++j){
cin >> n >> k;
if(k==4){ cout << abc(n) << '\n'; continue;}
else if(k>4){
string output = "";
for(i=0; i<k; ++i) output+="A";
output+=abc(n-k);
cout << output << '\n';
continue;
}
if((k==1 && n>2) || (k==2 && n>4) || (k==3 && n>9)) cout << "NIE\n";
else cout << solve(n, k) << '\n';
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include<bits/stdc++.h> using namespace std; int longest_palindrom(string s){ int output=1, i, l, r, n=s.size(); for(i=0; i<n; ++i){ l = i-1; r = i+1; while(l>=0 && r<n && s[l]==s[r]){ l--; r++; } output = max(output, (i-l)*2-1); l = i; r = i+1; while(l>=0 && r<n && s[l]==s[r]){ l--; r++; } output = max(output, (i-l)*2); } return output; } string solve(int n, int k){ int x = 1, a=1; for(int i=0; i<n; ++i){ x*=2; a+=x;} x = a; vector<string> s(x, ""); int i; for(i=1; i<x; ++i){ if(i%2!=0) s[i] = s[i/2]+'A'; else s[i] = s[(i-1)/2]+'P'; if((int)s[i].size() == n && longest_palindrom(s[i])==k) return s[i]; } return "NIE"; } string abc(int n){ string curr = "P"; int x = 1, i; while(x<n){ for(i=0; i<x; ++i){ if(curr[i]=='P') curr+="A"; else curr+="P"; } x*=2; } curr.resize(n); return curr; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin.tie(0); int n, k, i, t; cin >> t; for(int j=0; j<t; ++j){ cin >> n >> k; if(k==4){ cout << abc(n) << '\n'; continue;} else if(k>4){ string output = ""; for(i=0; i<k; ++i) output+="A"; output+=abc(n-k); cout << output << '\n'; continue; } if((k==1 && n>2) || (k==2 && n>4) || (k==3 && n>9)) cout << "NIE\n"; else cout << solve(n, k) << '\n'; } } |
English