#include <bits/stdc++.h>
using namespace std;
int K, Q, D, M;
vector<int> digits;
constexpr int IMPOSSIBLE = 6785;
constexpr int NOT_CALCULATED = -6785;
constexpr int C = (int)1e6;
map<long long, int> par;
vector<int> parr(C, NOT_CALCULATED);
bool backtrack(long long n) {
if (par.count(n)) return par[n] != IMPOSSIBLE;
for (int d : digits) {
if ((n - d) % K == 0 && backtrack((n - d) / K)) {
par[n] = d;
return true;
}
}
par[n] = IMPOSSIBLE;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> K >> Q >> D >> M;
digits.resize(D);
for (int i = 0; i < D; i++) {
cin >> digits[i];
}
for (int d : digits) {
par[d] = d;
}
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... |