#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 17;
int n, r, cursor;
int a[maxn];
void cal()
{
memset(a, 0, sizeof(a));
cursor = maxn - 1;
int flag, tmp, mod = -r, i;
tmp = n < 0 ? -n : n;
while (tmp) {
a[cursor--] = tmp % mod;
tmp /= mod;
}
flag = n > 0 ? 1 : 0;
for (i = maxn - 1; i > cursor; --i) {
if (i % 2 == flag && a[i] != 0) {
++a[i - 1];
a[i] = mod - a[i];
if (i - 1 == cursor)
--cursor;
}
if (a[i] >= mod) {
a[i - 1] += a[i] / mod;
a[i] %= mod;
if (i - 1 == cursor)
--cursor;
}
}
char c;
for (i = cursor + 1; i < maxn; ++i) {
if (a[i] >= 10) {
c = a[i] - 10 + 'A';
printf("%c", c);
} else
printf("%d", a[i]);
}
cout << endl;
}
int main()
{
//freopen("in.txt", "r", stdin);
while (cin >> n >> r) {
if (n == 0)
cout << 0 << endl;
else
cal();
}
return 0;
}
Sicily 1295. 负权数
最新推荐文章于 2017-09-10 12:46:37 发布