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>
using namespace std;
#define int long long
const long long inf = LLONG_MAX;
map<long long, long long> par;
int a[5008];
int d, K;
bool attempt(long long R) {
if (par.count(R)) return (par[R] != inf);
par[R] = inf;
for (int i = 1; i <= d; ++i) if (abs(R - a[i]) % K == 0)
if (R == a[i] || attempt((R - a[i]) / K)) {par[R] = a[i]; return 1;}
return 0;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int q, m; cin >> K >> q >> d >> m;
for (int i = 1; i <= d; ++i) cin >> a[i];
long long N;
while (q--) {
cin >> N;
if (!attempt(N)) cout << "IMPOSSIBLE\n";
else {
vector<int> ans; long long x;
while (1) {
x = par[N]; ans.push_back(x);
N = (N - x) / K;
if (N == 0) break;
}
reverse(ans.begin(), ans.end());
for (int i : ans) cout << i << ' ';
cout << '\n';
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |