제출 #1125084

#제출 시각아이디문제언어결과실행 시간메모리
1125084PanndaWeird Numeral System (CCO21_day1problem2)C++20
0 / 25
1 ms328 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int K, Q, D, M; cin >> K >> Q >> D >> M; vector<int> digits(D); for (int i = 0; i < D; i++) { cin >> digits[i]; } while (Q--) { long long N; cin >> N; map<long long, int> par; for (int d : digits) { par[d] = d; } const int IMPOSSIBLE = 6785; auto backtrack = [&](auto self, long long n) -> bool { if (par.count(n)) return par[n] != IMPOSSIBLE; for (int d : digits) { if ((n - d) % K == 0 && self(self, (n - d) / K)) { par[n] = d; return true; } } par[n] = IMPOSSIBLE; return false; }; if (!backtrack(backtrack, N)) { cout << "IMPOSSIBLE\n"; } else { vector<int> ans; while (N != 0) { int d = par[N]; ans.push_back(d); N = (N - d) / K; } 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...