0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-18 13:51:58 +00:00
OI-codes/Luogu/P1015/P1015.cpp

61 lines
1.3 KiB
C++
Raw Normal View History

2020-10-13 13:40:23 +00:00
#include <bits/stdc++.h>
using namespace std;
bool check(string s) {
string r = s;
reverse(r.begin(), r.end());
return r == s;
}
string add(string b, int k) {
2021-11-19 09:01:13 +00:00
string a = b;
char sixt[20] = "0123456789ABCDEF";
2020-10-13 13:40:23 +00:00
reverse(a.begin(), a.end());
2021-11-19 09:01:13 +00:00
int numa[105], numb[105], numc[105];
int len = a.length(), lenc = 1;
2020-10-13 13:40:23 +00:00
string ans;
for (int i = 0; i < len; i++) {
if (isdigit(a[i])) {
numa[len - i] = a[i] - '0';
2021-11-19 09:01:13 +00:00
} else {
2020-10-13 13:40:23 +00:00
numa[len - i] = a[i] - 'A' + 10;
}
if (isdigit(b[i])) {
numb[len - i] = b[i] - '0';
2021-11-19 09:01:13 +00:00
} else {
2020-10-13 13:40:23 +00:00
numb[len - i] = b[i] - 'A' + 10;
}
}
int x = 0;
while (lenc <= len) {
numc[lenc] = numa[lenc] + numb[lenc] + x;
2021-11-19 09:01:13 +00:00
x = numc[lenc] / k;
2020-10-13 13:40:23 +00:00
numc[lenc] %= k;
lenc++;
}
numc[lenc] = x;
while (numc[lenc] == 0) {
lenc--;
}
for (int i = lenc; i >= 1; i--) {
ans += sixt[numc[i]];
}
return ans;
}
int main() {
2021-11-19 09:01:13 +00:00
int n;
2020-10-13 13:40:23 +00:00
string m;
cin >> n >> m;
for (int i = 0; i <= 30; i++) {
if (check(m)) {
cout << "STEP=" << i << endl;
return 0;
}
m = add(m, n);
}
cout << "Impossible!" << endl;
return 0;
}