Nie sprawdzałem dla wszystkich wartości, ale wydaje mi się, że jest dobrze. Sprawdzam palindromiczność metodą macierzową, tj wpisuję cyfry do macierzy i obliczam jej wyznacznik. Jeśli jest zero, to znaczy że jest palindromem.
#include <iostream>
#include <sstream>
#include <vector>
template<typename T>
class simple_2d_matrix_view
{
T* data_;
size_t width_;
size_t height_;
public:
simple_2d_matrix_view(T* ptr, size_t h, size_t w):
data_{ptr},
width_{w},
height_{h}
{}
size_t width() const { return width_; }
size_t height() const { return height_; }
T& operator()(size_t h, size_t w) {
assert(w < width_);
assert(h < height_);
return data_[width_ * h + w];
}
T const& operator()(size_t h, size_t w) const {
return const_cast<simple_2d_matrix_view&>(*this)(h, w);
}
};
long long matrix_det(simple_2d_matrix_view<int> v)
{
assert(v.height() == v.width());
long long det = 0;
for(int i = 0; i < v.width(); i++) {
long long plus = 1, minus = 1;
for(int j = 0; j < v.height(); j++) {
int x_plus = (i + j) % v.height();
int x_minus = (i - j + v.height()) % v.height();
int y = j;
plus *= v(x_plus, y);
minus *= v(x_minus, y);
}
det += plus - minus;
}
return det;
}
bool is_palindromic(int val)
{
std::stringstream conv;
conv << val;
std::string tested = conv.str();
if (tested.size() == 1)
return true;
if (tested.size() == 2)
return tested.front() == tested.back();
std::vector<int> matrix(tested.size() * tested.size(), '0');
simple_2d_matrix_view<int> v(matrix.data(), tested.size(), tested.size());
for (int i = 0; i < tested.size(); i++)
v(i, 0) = tested[i];
for (int i = 0; i < tested.size(); i++)
v(0, i) = tested[i];
for (int i = 0; i < tested.size() - 1; i++)
v(tested.size() - 1, tested.size() - 1 - i) = tested[i];
for (int i = 0; i < tested.size() - 1; i++)
v(tested.size() - 1 - i, tested.size() - 1) = tested[i];
if (v.width() > 4) {
for (int i = 1; i < tested.size() - 1; i++)
v(i, i) = tested[i];
for (int i = 1; i < tested.size() - 1; i++)
v(i, tested.size() - 1 - i) = tested[i];
}
return matrix_det(v) == 0;
}
int main()
{
int val;
if (!(std::cin >> val))
return 1;
if (is_palindromic(val) == 0)
std::cout << "TAK\n";
else
std::cout << "NIE\n";
}