This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//8 out of 25 marks - no overflows possible
#include <bits/stdc++.h>
#define int long long int
signed main() {
int K, Q, D, M;
std::cin >> K >> Q >> D >> M;
std::set<int> digits;
for (int i = 0; i < D; ++i) {
int a;
std::cin >> a;
digits.insert(a);
}
for (int _ = 0; _ < Q; ++_) {
int number;
std::cin >> number;
int power = 1;
std::stack<int> result;
while (number) {
int next = (number % (power * K)) / power;
next = (next + std::abs(next) * K) % K;
int digit = 0;
if (digits.count(next)) {
digit = next;
} else if (digits.count(next - K)) {
digit = next - K;
} else {
goto broken;
}
result.push(digit);
number -= digit * power;
power *= K;
}
while (!result.empty()) {
std::cout << result.top() << " ";
result.pop();
}
std::cout << std::endl;
continue;
broken:
std::cout << "IMPOSSIBLE" << std::endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |