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