Submission #1102394

#TimeUsernameProblemLanguageResultExecution timeMemory
1102394adaawfWeird Numeral System (CCO21_day1problem2)C++17
0 / 25
5 ms23888 KiB
#include <iostream> #include <queue> #include <unordered_map> using namespace std; unordered_map<long long int, int> m; vector<int> g[1000005]; int k; int trya(long long int x) { if (x == 0) return 0; if (m.count(x)) return m[x]; long long int res = -1e9, h = (x % k + k) % k; for (int w : g[h]) { if ((x - w) / k == x) continue; int l = trya((x - w) / k); if (l != -1e9) { res = w; break; } } m[x] = res; return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); m[0] = 0; long long int q, d, ma; cin >> k >> q >> d >> ma; for (int i = 1; i <= d; i++) { long long int x, y; cin >> x; y = (x % k + k) % k; g[y].push_back(x); } for (int jj = 0; jj < q; jj++) { long long int x, y; cin >> x; y = trya(x); if (y == -1e9) { cout << "IMPOSSIBLE" << '\n'; continue; } vector<int> v; while (x) { v.push_back(m[x]); x = (x - m[x]) / k; } for (int i = v.size() - 1; i >= 0; i--) { cout << v[i] << " \n"[i+1==(int)v.size()]; } cout << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...