0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 23:36:32 +00:00

P1015 回文数

R39762388
This commit is contained in:
Baoshuo Ren 2020-10-13 21:40:23 +08:00 committed by Baoshuo Ren
parent 7b818a5baf
commit 6987c65221
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

62
problem/P1015/P1015.cpp Normal file
View File

@ -0,0 +1,62 @@
#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) {
string a = b;
char sixt[20] = "0123456789ABCDEF";
reverse(a.begin(), a.end());
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 += sixt[numc[i]];
}
return ans;
}
int main() {
int n;
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;
}