Submission #732574

# Submission time Handle Problem Language Result Execution time Memory
732574 2023-04-29T06:11:49 Z Programmer123 Weird Numeral System (CCO21_day1problem2) C++17
0 / 25
776 ms 1048576 KB
#include <bits/stdc++.h>
#define int long long int
#define IMPOSSIBLE 10000
std::set<int> digits;
int K, M;
std::map<int, int> answer;
int dp(int num) {
    if (answer.count(num)) return answer[num];
    if (digits.count(num)) {
        answer[num] = num;
        return num;
    }
//    else if (std::abs(num) < M) {
//        answer[num] = IMPOSSIBLE;
//        return IMPOSSIBLE;
//    }
    for (auto digit: digits) {
        int newNum = num - digit;
        if (newNum - K * (newNum / K) != 0) continue;
        if (dp(newNum / K) == IMPOSSIBLE) continue;
        answer[num] = digit;
        return digit;
    }
    answer[num] = IMPOSSIBLE;
    return IMPOSSIBLE;
}
signed main() {
    int Q, D;
    std::cin >> K >> Q >> D >> M;
    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 (dp(number) != IMPOSSIBLE) {
            std::stack<int> result;
            result.push(dp(number));
            number = (number - dp(number)) / K;
            while (number) {
                int digit = dp(number);
                result.push(digit);
                number -= digit;
                number /= K;
            }
            while (!result.empty()) {
                std::cout << result.top() << (result.size() > 1 ? " " : "");
                result.pop();
            }
            std::cout << std::endl;
        } else {
            std::cout << "IMPOSSIBLE" << std::endl;
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB OK
2 Correct 1 ms 212 KB OK
3 Correct 0 ms 212 KB OK
4 Correct 0 ms 212 KB OK
5 Correct 0 ms 212 KB OK
6 Correct 1 ms 212 KB OK
7 Runtime error 776 ms 1048576 KB Execution killed with signal 9
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB OK
2 Correct 1 ms 212 KB OK
3 Correct 0 ms 212 KB OK
4 Correct 0 ms 212 KB OK
5 Correct 0 ms 212 KB OK
6 Correct 1 ms 212 KB OK
7 Runtime error 776 ms 1048576 KB Execution killed with signal 9
8 Halted 0 ms 0 KB -