#include <array>
#include <bitset>
#include <cstdio>
bool palindrom(int N, bool* slowo)
{
for (int i=0, j=N-1; i<j; ++i, --j) {
if (slowo[i] != slowo[j]) {
return false;
}
}
return true;
}
int max_palindrom(int N, bool* slowo)
{
for (int K=N; K>=2; --K) {
for (int i=0; i<=N-K; ++i) {
if (palindrom(K, slowo+i)) {
return K;
}
}
}
return 1;
}
template<int P>
class Generator
{
std::bitset<P> pattern;
public:
Generator(unsigned seed) : pattern(seed)
{ }
bool generate(int N, int K, bool* slowo)
{
if (K < 4 || K > N) {
return false;
}
for (int i=0; i<K; ++i) {
slowo[i] = false;
}
for (int i=K; i<N; ++i) {
slowo[i] = pattern[(i-K)%P];
}
return true;
}
};
template<int MAX_N>
class BrutFors
{
const unsigned NIE = 0xFFFFFFFFu;
std::array<std::array<unsigned, MAX_N+1>, MAX_N+1> cache;
public:
BrutFors()
{
std::array<bool, MAX_N> slowo;
for (int N=0; N<=MAX_N; ++N) {
for (int K=0; K<=MAX_N; ++K) {
cache[N][K] = NIE;
}
}
for (int N=1; N<=MAX_N; ++N) {
const unsigned seed_end = 1 << N;
for (unsigned seed=0; seed<seed_end; ++seed) {
std::bitset<MAX_N> bits(seed);
for (int i=0; i<N; ++i) slowo[i] = bits[i];
int K = max_palindrom(N, slowo.data());
if (K > 0) {
cache[N][K] = seed;
}
}
}
}
bool generate(int N, int K, bool* slowo)
{
if (N > MAX_N || cache[N][K] == NIE) {
return false;
}
std::bitset<MAX_N> bits(cache[N][K]);
for (int i=0; i<N; ++i) slowo[i] = bits[i];
return true;
}
};
class MultiGenerator
{
BrutFors<8> small;
Generator<6> large;
public:
MultiGenerator() : large(11)
{ }
bool generate(int N, int K, bool* slowo)
{
if (N <= 8) {
return small.generate(N, K, slowo);
} else {
return large.generate(N, K, slowo);
}
}
};
void wypisz(int N, bool* slowo)
{
for (int i=0; i<N; ++i) {
putchar(slowo[i] ? 'P' : 'A');
}
putchar('\n');
}
int main()
{
bool* slowo = new bool[1000000];
MultiGenerator g;
int T; scanf("%d", &T);
while (T-->0) {
int N, K;
scanf("%d%d", &N, &K);
if (g.generate(N, K, slowo)) {
wypisz(N, slowo);
} else {
puts("NIE");
}
}
}
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 | #include <array> #include <bitset> #include <cstdio> bool palindrom(int N, bool* slowo) { for (int i=0, j=N-1; i<j; ++i, --j) { if (slowo[i] != slowo[j]) { return false; } } return true; } int max_palindrom(int N, bool* slowo) { for (int K=N; K>=2; --K) { for (int i=0; i<=N-K; ++i) { if (palindrom(K, slowo+i)) { return K; } } } return 1; } template<int P> class Generator { std::bitset<P> pattern; public: Generator(unsigned seed) : pattern(seed) { } bool generate(int N, int K, bool* slowo) { if (K < 4 || K > N) { return false; } for (int i=0; i<K; ++i) { slowo[i] = false; } for (int i=K; i<N; ++i) { slowo[i] = pattern[(i-K)%P]; } return true; } }; template<int MAX_N> class BrutFors { const unsigned NIE = 0xFFFFFFFFu; std::array<std::array<unsigned, MAX_N+1>, MAX_N+1> cache; public: BrutFors() { std::array<bool, MAX_N> slowo; for (int N=0; N<=MAX_N; ++N) { for (int K=0; K<=MAX_N; ++K) { cache[N][K] = NIE; } } for (int N=1; N<=MAX_N; ++N) { const unsigned seed_end = 1 << N; for (unsigned seed=0; seed<seed_end; ++seed) { std::bitset<MAX_N> bits(seed); for (int i=0; i<N; ++i) slowo[i] = bits[i]; int K = max_palindrom(N, slowo.data()); if (K > 0) { cache[N][K] = seed; } } } } bool generate(int N, int K, bool* slowo) { if (N > MAX_N || cache[N][K] == NIE) { return false; } std::bitset<MAX_N> bits(cache[N][K]); for (int i=0; i<N; ++i) slowo[i] = bits[i]; return true; } }; class MultiGenerator { BrutFors<8> small; Generator<6> large; public: MultiGenerator() : large(11) { } bool generate(int N, int K, bool* slowo) { if (N <= 8) { return small.generate(N, K, slowo); } else { return large.generate(N, K, slowo); } } }; void wypisz(int N, bool* slowo) { for (int i=0; i<N; ++i) { putchar(slowo[i] ? 'P' : 'A'); } putchar('\n'); } int main() { bool* slowo = new bool[1000000]; MultiGenerator g; int T; scanf("%d", &T); while (T-->0) { int N, K; scanf("%d%d", &N, &K); if (g.generate(N, K, slowo)) { wypisz(N, slowo); } else { puts("NIE"); } } } |
English