This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long int
#define IMPOSSIBLE 10000
std::unordered_set<int> digits;
int K, M;
std::unordered_map<int, int> answer;
int dp(int num) {
if (answer.count(num)) return answer[num];
if (digits.count(num)) {
answer[num] = num;
return num;
}
answer[num] = IMPOSSIBLE;
for (auto digit: digits) {
int newNum = num - digit;
if (newNum % K != 0) continue;
if (dp(newNum / K) == IMPOSSIBLE) continue;
answer[num] = digit;
return digit;
}
return IMPOSSIBLE;
}
signed main() {
int Q, D;
std::cin >> K >> Q >> D >> M;
for (int i = 0; i < D; ++i) {
int a;
std::cin >> a;
digits.insert(a);
}
for (int _ = 0; _ < Q; ++_) {
int number;
std::cin >> number;
if (dp(number) != IMPOSSIBLE) {
std::stack<int> result;
result.push(dp(number));
number = (number - dp(number)) / K;
while (number) {
int digit = dp(number);
result.push(digit);
number -= digit;
number /= K;
}
while (!result.empty()) {
std::cout << result.top() << (result.size() > 1 ? " " : "");
result.pop();
}
std::cout << std::endl;
} else {
std::cout << "IMPOSSIBLE" << std::endl;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |