Submission #732527

#TimeUsernameProblemLanguageResultExecution timeMemory
732527Programmer123Weird Numeral System (CCO21_day1problem2)C++17
0 / 25
0 ms212 KiB
//8 out of 25 marks - no overflows possible #include <bits/stdc++.h> #define int long long int signed main() { int K, Q, D, M; std::cin >> K >> Q >> D >> M; std::set<int> digits; for (int i = 0; i < D; ++i) { int a; std::cin >> a; digits.insert(a); } for (int _ = 0; _ < Q; ++_) { int number; std::cin >> number; if (number == 0) { if (digits.count(0)) { std::cout << "0" << std::endl; } else { std::cout << "IMPOSSIBLE" << std::endl; } continue; } std::stack<int> result; while (number) { int next = (number % K); next = (next + std::abs(next) * K) % K; int digit; if (digits.count(next)) { digit = next; } else if (digits.count(next - K)) { digit = next - K; } else if (digits.count(next + K)) { digit = next + K; } else { goto broken; } result.push(digit); number -= digit; number /= K; } while (!result.empty()) { std::cout << result.top() << " "; result.pop(); } std::cout << std::endl; continue; broken: std::cout << "IMPOSSIBLE" << std::endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...