#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool is_palindrome(const string& s, int start, int end) {
while (start < end) {
if (s[start] != s[end]) return false;
start++;
end--;
}
return true;
}
void solve() {
int n, k;
if (!(cin >> n >> k)) return;
if (k == 1) {
if (n == 1) cout << "P\n";
else if (n == 2) cout << "PA\n";
else cout << "NIE\n";
return;
}
if (k > n) {
cout << "NIE\n";
return;
}
string s = "";
for (int i = 0; i < k; ++i) s += 'A';
for (int i = k; i < n; ++i) {
char candidates[] = {'P', 'A'};
bool found = false;
for (char c : candidates) {
s.push_back(c);
bool creates_long_pal = false;
for (int len = k + 1; len <= i + 1; ++len) {
if (is_palindrome(s, i + 1 - len, i)) {
creates_long_pal = true;
break;
}
}
if (!creates_long_pal) {
found = true;
break;
}
s.pop_back();
}
if (!found) {
cout << "NIE\n";
return;
}
}
cout << s << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
if (!(cin >> t)) return 0;
while (t--) {
solve();
}
return 0;
}
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 | #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; bool is_palindrome(const string& s, int start, int end) { while (start < end) { if (s[start] != s[end]) return false; start++; end--; } return true; } void solve() { int n, k; if (!(cin >> n >> k)) return; if (k == 1) { if (n == 1) cout << "P\n"; else if (n == 2) cout << "PA\n"; else cout << "NIE\n"; return; } if (k > n) { cout << "NIE\n"; return; } string s = ""; for (int i = 0; i < k; ++i) s += 'A'; for (int i = k; i < n; ++i) { char candidates[] = {'P', 'A'}; bool found = false; for (char c : candidates) { s.push_back(c); bool creates_long_pal = false; for (int len = k + 1; len <= i + 1; ++len) { if (is_palindrome(s, i + 1 - len, i)) { creates_long_pal = true; break; } } if (!creates_long_pal) { found = true; break; } s.pop_back(); } if (!found) { cout << "NIE\n"; return; } } cout << s << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; if (!(cin >> t)) return 0; while (t--) { solve(); } return 0; } |
English