#include <bits/stdc++.h>
using namespace std;
#define TF(i, p, q) for(int i = (p); i <= (q); ++i)
#define TR(i, q, p) for(int i = (q); i >= (p); --i)
#define V vector
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define th third
#define rz resize
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()
#define sz(V) (int)(V.size())
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b);
typedef pair<int,int> ii;
typedef queue<int> qi;
typedef queue<ii> qii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>>vvi;
typedef vector<vector<bool>>vvb;
typedef vector<bool> vb;
typedef vector<ii> vii;
constexpr int inf = 1e9 + 9;
int manacher(int n, vi& tab){
vi mp(n);
int l = -1, r = 0;
int i = 0;
int score = 0;
while(i < n){
int k = 0;
int alt = l+(r-i);
if(alt < 0) k = 0;
else k = mp[alt];
if(i+k >= r){
r = i;
while(r < n and i+i-r >= 0 and tab[r] == tab[i+i-r]){
r++;
}
r--;
mp[i] = r-i;
l = i+i-r;
}
else mp[i] = k;
cmax(score, mp[i] + (tab[r] > 0 ? 1 : 0));
i++;
}
return score;
}
int check(int n, vi ntab){
vi tab;
TF(i, 0, n-1){
tab.pb(ntab[i]);
tab.pb(-1);
}
int xd = manacher(sz(tab), tab);
return xd;
}
V<V<string>>ants(4, V<string>(9));
void prep(){
TF(l, 1, 2){
int n = l, k = 1;
TF(i, 0, (1<<n)-1){
vi tab;
TF(j, 0, n-1){
if(i&(1<<j)) tab.pb(1);
else tab.pb(2);
}
int xd = check(n, tab);
if(xd == k){
string ans;
TF(j, 0, n-1){
if(tab[j] == 1) ans = ans + "P";
else ans = ans + "A";
}
ants[1][l] = ans;
}
else continue;
break;
}
}
TF(l, 1, 4){
int n = l, k = 2;
TF(i, 0, (1<<n)-1){
vi tab;
TF(j, 0, n-1){
if(i&(1<<j)) tab.pb(1);
else tab.pb(2);
}
int xd = check(n, tab);
if(xd == k){
string ans;
TF(j, 0, n-1){
if(tab[j] == 1) ans = ans + "P";
else ans = ans + "A";
}
ants[2][l] = ans;
}
else continue;
break;
}
}
TF(l, 1, 8){
int n = l, k = 3;
TF(i, 0, (1<<n)-1){
vi tab;
TF(j, 0, n-1){
if(i&(1<<j)) tab.pb(1);
else tab.pb(2);
}
int xd = check(n, tab);
if(xd == k){
string ans;
TF(j, 0, n-1){
if(tab[j] == 1) ans = ans + "P";
else ans = ans + "A";
}
ants[3][l] = ans;
}
else continue;
break;
}
}
}
void solve(){
int n, k; cin >> n >> k;
if(k < 4){
if(k == 1 and n > 2){
cout << "NIE" << endl;
return;
}
else if(k == 2 and n > 4){
cout << "NIE" << endl;
return;
}
else if(k == 3 and n > 8){
cout << "NIE" << endl;
return;
}
else if(k > n){
cout << "NIE" << endl;
return;
}
cout << ants[k][n] << endl;
return;
}
else{
if(n < k){
cout << "NIE" << endl;
return;
}
else{
vi cyc = {0, 1, 0, 0, 1, 1};
int incyc = 0;
int cyclength = sz(cyc);
TF(i, 0, k-1) cout << "A";
TF(i, k, n-1){
int xd = cyc[incyc];
if(xd) cout << "A";
else cout << "P";
incyc++; if(incyc >= cyclength) incyc -= cyclength;
}
cout << endl;
return;
}
}
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0); prep(); int t; cin >> t;
while(t--) solve();
}
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | #include <bits/stdc++.h> using namespace std; #define TF(i, p, q) for(int i = (p); i <= (q); ++i) #define TR(i, q, p) for(int i = (q); i >= (p); --i) #define V vector #define pb push_back #define eb emplace_back #define fi first #define se second #define th third #define rz resize #define all(V) V.begin(), V.end() #define rall(V) V.rbegin(), V.rend() #define sz(V) (int)(V.size()) #define cmin(a, b) a = min(a, b) #define cmax(a, b) a = max(a, b); typedef pair<int,int> ii; typedef queue<int> qi; typedef queue<ii> qii; typedef long long ll; typedef vector<int> vi; typedef vector<vector<int>>vvi; typedef vector<vector<bool>>vvb; typedef vector<bool> vb; typedef vector<ii> vii; constexpr int inf = 1e9 + 9; int manacher(int n, vi& tab){ vi mp(n); int l = -1, r = 0; int i = 0; int score = 0; while(i < n){ int k = 0; int alt = l+(r-i); if(alt < 0) k = 0; else k = mp[alt]; if(i+k >= r){ r = i; while(r < n and i+i-r >= 0 and tab[r] == tab[i+i-r]){ r++; } r--; mp[i] = r-i; l = i+i-r; } else mp[i] = k; cmax(score, mp[i] + (tab[r] > 0 ? 1 : 0)); i++; } return score; } int check(int n, vi ntab){ vi tab; TF(i, 0, n-1){ tab.pb(ntab[i]); tab.pb(-1); } int xd = manacher(sz(tab), tab); return xd; } V<V<string>>ants(4, V<string>(9)); void prep(){ TF(l, 1, 2){ int n = l, k = 1; TF(i, 0, (1<<n)-1){ vi tab; TF(j, 0, n-1){ if(i&(1<<j)) tab.pb(1); else tab.pb(2); } int xd = check(n, tab); if(xd == k){ string ans; TF(j, 0, n-1){ if(tab[j] == 1) ans = ans + "P"; else ans = ans + "A"; } ants[1][l] = ans; } else continue; break; } } TF(l, 1, 4){ int n = l, k = 2; TF(i, 0, (1<<n)-1){ vi tab; TF(j, 0, n-1){ if(i&(1<<j)) tab.pb(1); else tab.pb(2); } int xd = check(n, tab); if(xd == k){ string ans; TF(j, 0, n-1){ if(tab[j] == 1) ans = ans + "P"; else ans = ans + "A"; } ants[2][l] = ans; } else continue; break; } } TF(l, 1, 8){ int n = l, k = 3; TF(i, 0, (1<<n)-1){ vi tab; TF(j, 0, n-1){ if(i&(1<<j)) tab.pb(1); else tab.pb(2); } int xd = check(n, tab); if(xd == k){ string ans; TF(j, 0, n-1){ if(tab[j] == 1) ans = ans + "P"; else ans = ans + "A"; } ants[3][l] = ans; } else continue; break; } } } void solve(){ int n, k; cin >> n >> k; if(k < 4){ if(k == 1 and n > 2){ cout << "NIE" << endl; return; } else if(k == 2 and n > 4){ cout << "NIE" << endl; return; } else if(k == 3 and n > 8){ cout << "NIE" << endl; return; } else if(k > n){ cout << "NIE" << endl; return; } cout << ants[k][n] << endl; return; } else{ if(n < k){ cout << "NIE" << endl; return; } else{ vi cyc = {0, 1, 0, 0, 1, 1}; int incyc = 0; int cyclength = sz(cyc); TF(i, 0, k-1) cout << "A"; TF(i, k, n-1){ int xd = cyc[incyc]; if(xd) cout << "A"; else cout << "P"; incyc++; if(incyc >= cyclength) incyc -= cyclength; } cout << endl; return; } } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); prep(); int t; cin >> t; while(t--) solve(); } |
English