Submission #1125089

#TimeUsernameProblemLanguageResultExecution timeMemory
1125089PanndaWeird Numeral System (CCO21_day1problem2)C++20
0 / 25
1377 ms1114112 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...