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
map<long long, pair<int, int>> dp;
int a[5008];
int d, K;
int attempt(long long R) {
if (dp.count(R)) return dp[R].first;
dp[R] = make_pair(1, 0);
for (int i = 1; i <= d; ++i) if (R == a[i]) {dp[R] = make_pair(2, i); return 2;}
for (int i = 1; i <= d; ++i) if (abs(R - a[i]) % K == 0) {
if (attempt((R - a[i]) / K) == 2) {dp[R] = make_pair(2, i); return 2;}
}
return 1;
}
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; bool pass;
while (q--) {
cin >> N; pass = 0; dp.clear();
for (int i = 1; i <= d; ++i) if (attempt(N) == 2) {pass = 1; break;}
if (!pass) cout << "IMPOSSIBLE\n";
else {
vector<int> ans; int x;
if (N == 0) {ans.push_back(a[dp[0].second]); N = a[dp[0].second];}
while (N != 0) {
x = a[dp[N].second]; ans.push_back(x);
N = (N - x) / K;
}
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... |