#include <bits/stdc++.h>
using namespace std;
int K, Q, D, M;
int digits[5001];
constexpr int IMPOSSIBLE = 6785;
map<long long, int> par;
bool backtrack(long long n) {
if (par.count(n)) return par[n] != IMPOSSIBLE;
for (int i = 0; i < D; i++) {
if ((n - digits[i]) % K == 0 && backtrack((n - digits[i]) / K)) {
par[n] = digits[i];
return true;
}
}
par[n] = IMPOSSIBLE;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> K >> Q >> D >> M;
for (int i = 0; i < D; i++) {
cin >> digits[i];
par[digits[i]] = digits[i];
}
while (Q--) {
long long N;
cin >> N;
if (!backtrack(N)) {
cout << "IMPOSSIBLE\n";
} else {
vector<int> ans;
do {
int d = par[N];
ans.push_back(d);
N = (N - d) / K;
} while (N != 0);
for (int i = ans.size() - 1; i >= 0; i--) {
cout << ans[i] << " \n"[i == 0];
}
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |