#include <cstdio>
#include <vector>
#ifdef LOCAL
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#else
#define dbg(...)
#endif
using namespace std;
bool handle_small(int n, int k) {
if (k == 1) {
if (n == 1) {
printf("A\n");
} else if (n == 2) {
printf("AP\n");
} else {
printf("NIE\n");
}
return true;
}
if (k == 2) {
if (n == 2) {
printf("AA\n");
} else if (n == 3) {
printf("AAP\n");
} else if (n == 4) {
printf("AAPP\n");
} else {
printf("NIE\n");
}
return true;
}
if (k == 3) {
if (n == 3) {
printf("AAA\n");
} else if (n == 4) {
printf("AAAP\n");
} else if (n == 5) {
printf("AAPPP\n");
} else if (n == 6) {
printf("AAPAPP\n");
} else if (n == 7) {
printf("AAPAPPP\n");
} else if (n == 8) {
printf("AAAPAPPP\n");
} else {
printf("NIE\n");
}
return true;
}
if (n < 4) {
printf("NIE\n");
return true;
}
return false;
}
int main() {
int t;
scanf("%d", &t);
for (int ti = 0; ti < t; ti++) {
int n, k;
scanf("%d %d", &n, &k);
if (k > n) {
printf("NIE\n");
continue;
}
bool handled = handle_small(n, k);
if (handled) continue;
// NOW WE HAVE NEED A PALINDROME OF LENGTH AT LEAST 4
for (int i = 0; i < k; ++i) {
printf("A");
}
if (k == n) {
printf("\n");
continue;
}
printf("P");
vector<int> last_chars({0,0,0,0,1});
for (int i = k+1; i < n; ++i) {
unsigned long last_idx = last_chars.size() - 1;
int nextchar = 0;
int last[5];
for (int j = 0; j < 5; ++j) {
last[j] = last_chars[last_idx - j];
}
if (last[0] == last[1]) {
// danger of going 3-of-a-kind
nextchar = 1 - last[0];
} else if (last[0] == last[2]) {
// 3-palindrome present - must avoid creating 5-palindrome
nextchar = 1 - last[3];
} else if (last[0] == last[3] && last[1] == last[2]) {
// 4-palindrome present - must avoid creating 6-palindrome
nextchar = 1 - last[4];
}
last_chars.push_back(nextchar);
if (nextchar == 0) {
printf("A");
} else {
printf("P");
}
}
printf("\n");
}
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include <cstdio> #include <vector> #ifdef LOCAL #define dbg(...) fprintf(stderr, __VA_ARGS__) #else #define dbg(...) #endif using namespace std; bool handle_small(int n, int k) { if (k == 1) { if (n == 1) { printf("A\n"); } else if (n == 2) { printf("AP\n"); } else { printf("NIE\n"); } return true; } if (k == 2) { if (n == 2) { printf("AA\n"); } else if (n == 3) { printf("AAP\n"); } else if (n == 4) { printf("AAPP\n"); } else { printf("NIE\n"); } return true; } if (k == 3) { if (n == 3) { printf("AAA\n"); } else if (n == 4) { printf("AAAP\n"); } else if (n == 5) { printf("AAPPP\n"); } else if (n == 6) { printf("AAPAPP\n"); } else if (n == 7) { printf("AAPAPPP\n"); } else if (n == 8) { printf("AAAPAPPP\n"); } else { printf("NIE\n"); } return true; } if (n < 4) { printf("NIE\n"); return true; } return false; } int main() { int t; scanf("%d", &t); for (int ti = 0; ti < t; ti++) { int n, k; scanf("%d %d", &n, &k); if (k > n) { printf("NIE\n"); continue; } bool handled = handle_small(n, k); if (handled) continue; // NOW WE HAVE NEED A PALINDROME OF LENGTH AT LEAST 4 for (int i = 0; i < k; ++i) { printf("A"); } if (k == n) { printf("\n"); continue; } printf("P"); vector<int> last_chars({0,0,0,0,1}); for (int i = k+1; i < n; ++i) { unsigned long last_idx = last_chars.size() - 1; int nextchar = 0; int last[5]; for (int j = 0; j < 5; ++j) { last[j] = last_chars[last_idx - j]; } if (last[0] == last[1]) { // danger of going 3-of-a-kind nextchar = 1 - last[0]; } else if (last[0] == last[2]) { // 3-palindrome present - must avoid creating 5-palindrome nextchar = 1 - last[3]; } else if (last[0] == last[3] && last[1] == last[2]) { // 4-palindrome present - must avoid creating 6-palindrome nextchar = 1 - last[4]; } last_chars.push_back(nextchar); if (nextchar == 0) { printf("A"); } else { printf("P"); } } printf("\n"); } return 0; } |
English