-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1112.cpp
48 lines (44 loc) · 843 Bytes
/
1112.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <algorithm>
#include <vector>
typedef long long ll;
using namespace std;
int main(){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
ll x, y;
cin >> x >> y;
if (x < 0 && y > 0) cout << '-';
if (x == 0) {
cout << 0;
return 0;
}
ll z = y;
int i = 1;
vector<ll> result;
while (x) {
ll c = 0;
if (y > 0 && x > 0) {
result.push_back(x%y);
} else if ((z > 0) ^ (x < 0)) {
c = abs(y) - abs(x % y);
c = abs(x % y) == 0 ? 0 : c;
result.push_back(c);
} else {
result.push_back(x%y);
}
if (c) {
ll xx = x < 0 ? -1 : 1;
x /= abs(y);
x += xx;
} else {
x /= abs(y);
}
z *= y;
i++;
}
reverse(result.begin(), result.end());
for (auto r : result) cout << abs(r);
return 0;
}