not_exist @ 2023-01-05 18:30:32
#include<bits/stdc++.h>
using namespace std;
char q[17] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
string o;
bool huiwen(string m) {
for (int i = 0; i < m.length() / 2; i++) {
if (m[i] != m[m.length() - 1 - i]) {
return 0;
}
}
return 1;
}
string swaps(string q) {
for (int i = 0; i < q.length() / 2; i++) {
swap(q[i], q[q.length() - 1 - i]);
}
return q;
}
string add(string a, string b, int k) {
int numa[105], numb[105], numc[105];
int len = a.length(), lenc = 1;
string ans;
for (int i = 0; i < len; i++) {
if (isdigit(a[i])) numa[len - i] = a[i] - '0';
else numa[len - i] = a[i] - 'A' + 10;
if (isdigit(b[i])) numb[len - i] = b[i] - '0';
else numb[len - i] = b[i] - 'A' + 10;
}
int x = 0;
while (lenc <= len) {
numc[lenc] = numa[lenc] + numb[lenc] + x;
x = numc[lenc] / k;
numc[lenc] %= k;
lenc++;
}
numc[lenc] = x;
while (numc[lenc] == 0) lenc--;
for (int i = lenc; i >= 1; i--) ans += q[numc[i]];
return ans;
}
int main() {
int jw;
cin >> jw;
cin >> o;
for (int i = 1; i <= 31; i++) {
if (huiwen(o)) {
cout << i - 1;
return 0;
}
if (i == 31) {
cout << "Impossible!";
return 0;
}
o = add(swaps(o), o, jw);
}
return 0;
}