Submission #635751

# Submission time Handle Problem Language Result Execution time Memory
635751 2022-08-26T18:37:55 Z jhnah917 Weird Numeral System (CCO21_day1problem2) C++14
0 / 25
1 ms 468 KB
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
constexpr int X = 5000;

int K, Q, N, M, A[5050], D[10101], P[10101], V[10101];
vector<int> R;
gp_hash_table<ll, int> Chk, Prv;

void BFS(){
    memset(D, -1, sizeof D);
    memset(P, -1, sizeof P);
    queue<int> que; que.push(0); D[X] = 0;
    while(!que.empty()){
        int v = que.front(); que.pop();
        for(int i=1; i<=N; i++){
            ll nxt = 1LL * v * K + A[i];
            if(abs(nxt) > M || D[nxt+X] != -1) continue;
            D[nxt+X] = D[v] + 1; P[nxt+X] = v+X; V[nxt+X] = A[i];
            que.push(nxt+X);
        }
    }
}

int DFS(ll n){
    auto it = Chk.find(n); if(it != Chk.end()) return it->second;
    if(abs(n) <= M) return Chk[n] = D[n+X] != -1;
    for(int i=1; i<=N; i++){
        if((n - A[i]) % K == 0 && DFS((n - A[i]) / K)){
            Prv[n] = A[i]; return Chk[n] = 1;
        }
    }
    return Chk[n] = 0;
}

vector<int> Track(ll n){
    vector<int> res;
    while(n != 0){
        int now = abs(n) <= M ? V[n+X] : Prv[n];
        res.push_back(now);
        n = (n - now) / K;
    }
    reverse(res.begin(), res.end());
    return res;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    cin >> K >> Q >> N >> M;
    for(int i=1; i<=N; i++) cin >> A[i];
    BFS();
    for(int q=1; q<=Q; q++){
        ll n; cin >> n;
        if(!DFS(n)){ cout << "IMPOSSIBLE\n"; continue; }
        auto vec = Track(n);
        for(int i=0; i<vec.size(); i++) cout << vec[i] << " \n"[i+1==vec.size()];
    }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for(int i=0; i<vec.size(); i++) cout << vec[i] << " \n"[i+1==vec.size()];
      |                      ~^~~~~~~~~~~
Main.cpp:57:68: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for(int i=0; i<vec.size(); i++) cout << vec[i] << " \n"[i+1==vec.size()];
      |                                                                 ~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB OK
2 Incorrect 1 ms 328 KB Query 1: Jury has an answer but participant does not
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB OK
2 Incorrect 1 ms 328 KB Query 1: Jury has an answer but participant does not
3 Halted 0 ms 0 KB -