#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;
if (R == 0) {
dp[R] = make_pair(2, 0);
return 2;
}
dp[R] = make_pair(1, 0);
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, a[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;
for (int i = 1; i <= d; ++i) if (attempt(N) == 2) {pass = 1; break;}
if (!pass) cout << "IMPOSSIBLE\n";
else {
vector<int> ans;
while (N) {
ans.push_back(dp[N].second);
N = (N - dp[N].second) / K;
}
reverse(ans.begin(), ans.end());
for (int i : ans) cout << i << ' ';
cout << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Expected integer, but "IMPOSSIBLE" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Expected integer, but "IMPOSSIBLE" found |
2 |
Halted |
0 ms |
0 KB |
- |