#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define FORD(i,a,b) for(int i = (a); i >= (b); i--)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); }
template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); }
template<class TH> void _dbg(const char *sdbg, TH h){ cerr << sdbg << '=' << h << endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
while(*sdbg!=',')cerr<<*sdbg++;
cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(CC) {cerr<<#CC<<"=";for(auto&cc:CC)cerr<<cc<<",";cerr<<endl;}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(CC) {}
#define cerr if(0)cout
#endif
struct Manacher {
VI par, npar;
int n;
Manacher(string in) { // IGNORES FIRST LETTER - in[0] must be space
n = SZ(in);
assert(in[0] == ' '); // par[i] = k <=> [i - k + 1, i + k] is maximal palindrome
int orig_n = SZ(in) - 1; // npar[i] = k <=> [i - k, i + k] is maximal palindrome
string s = " #";
for (int i = 1; i <= orig_n; i++) {
s += in[i]; s += '#';
}
s += '$';
int new_n = SZ(s) - 2;
npar.resize(new_n + 2);
int furth_beg = 0; int furth_end = 0;
for (int i = 1; i <= new_n; i++) {
if (furth_end < i) { furth_beg = i; furth_end = i; }
int corr_npar = furth_beg + furth_end - i;
if (furth_end > i + npar[corr_npar]) {
npar[i] = npar[corr_npar];
} else {
npar[i] = furth_end - i;
furth_beg = i - npar[i];
while (s[furth_beg - 1] == s[furth_end + 1]) {
furth_beg--; furth_end++; npar[i]++;
}
}
}
par.resize(orig_n + 2);
for (int i = 1; i <= orig_n; i++) {
if (i < orig_n) {
par[i] = npar[2 * i + 1] / 2;
}
npar[i] = npar[2 * i] / 2;
}
npar.resize(orig_n + 2);
}
int max_palindrome() {
int orig_n = n - 1;
int res = 1;
FOR(i, 1, orig_n){
maxi(res, 2 * npar[i] + 1);
if(i < orig_n) maxi(res, 2 * par[i]);
}
return res;
}
};
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(11);
cerr << fixed << setprecision(6);
int t;
cin >> t;
REP(_, t){
int n, k;
cin >> n >> k;
if(n <= 10){
for (int mask = 0; mask < (1 << n); mask++) {
string s(n, 'P');
for (int i = 0; i < n; i++){
if (mask >> i & 1) s[i] = 'A';
}
int lp = Manacher(string(" ") + s).max_palindrome();
if(lp == k){
cout << s << "\n";
goto nxt;
}
}
cout << "NIE\n";
} else{
if(k < 4){
cout << "NIE\n";
} else{
string s(k, 'A');
while(SZ(s) < n){
s += "PAPPAA";
}
s.resize(n);
assert(Manacher(string(" ") + s).max_palindrome() == k);
cout << s << "\n";
}
}
nxt:;
}
}
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | #include <bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); } template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr << sdbg << '=' << h << endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(CC) {cerr<<#CC<<"=";for(auto&cc:CC)cerr<<cc<<",";cerr<<endl;} #else #define debug(...) (__VA_ARGS__) #define debugv(CC) {} #define cerr if(0)cout #endif struct Manacher { VI par, npar; int n; Manacher(string in) { // IGNORES FIRST LETTER - in[0] must be space n = SZ(in); assert(in[0] == ' '); // par[i] = k <=> [i - k + 1, i + k] is maximal palindrome int orig_n = SZ(in) - 1; // npar[i] = k <=> [i - k, i + k] is maximal palindrome string s = " #"; for (int i = 1; i <= orig_n; i++) { s += in[i]; s += '#'; } s += '$'; int new_n = SZ(s) - 2; npar.resize(new_n + 2); int furth_beg = 0; int furth_end = 0; for (int i = 1; i <= new_n; i++) { if (furth_end < i) { furth_beg = i; furth_end = i; } int corr_npar = furth_beg + furth_end - i; if (furth_end > i + npar[corr_npar]) { npar[i] = npar[corr_npar]; } else { npar[i] = furth_end - i; furth_beg = i - npar[i]; while (s[furth_beg - 1] == s[furth_end + 1]) { furth_beg--; furth_end++; npar[i]++; } } } par.resize(orig_n + 2); for (int i = 1; i <= orig_n; i++) { if (i < orig_n) { par[i] = npar[2 * i + 1] / 2; } npar[i] = npar[2 * i] / 2; } npar.resize(orig_n + 2); } int max_palindrome() { int orig_n = n - 1; int res = 1; FOR(i, 1, orig_n){ maxi(res, 2 * npar[i] + 1); if(i < orig_n) maxi(res, 2 * par[i]); } return res; } }; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); int t; cin >> t; REP(_, t){ int n, k; cin >> n >> k; if(n <= 10){ for (int mask = 0; mask < (1 << n); mask++) { string s(n, 'P'); for (int i = 0; i < n; i++){ if (mask >> i & 1) s[i] = 'A'; } int lp = Manacher(string(" ") + s).max_palindrome(); if(lp == k){ cout << s << "\n"; goto nxt; } } cout << "NIE\n"; } else{ if(k < 4){ cout << "NIE\n"; } else{ string s(k, 'A'); while(SZ(s) < n){ s += "PAPPAA"; } s.resize(n); assert(Manacher(string(" ") + s).max_palindrome() == k); cout << s << "\n"; } } nxt:; } } |
English