Submission #635759

#TimeUsernameProblemLanguageResultExecution timeMemory
635759jhnah917Weird Numeral System (CCO21_day1problem2)C++14
8 / 25
2582 ms2836 KiB
#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]; gp_hash_table<ll, int> Chk, Prv; void BFS(){ memset(D, -1, sizeof D); memset(P, -1, sizeof P); queue<ll> que; que.push(0); D[X] = 0; while(!que.empty()){ ll v = que.front(); que.pop(); for(int i=1; i<=N; i++){ ll nxt = v * K + A[i]; if(abs(nxt) > M || D[nxt+X] != -1) continue; D[nxt+X] = D[v+X] + 1; P[nxt+X] = A[i]; que.push(nxt); } } } 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 ? P[n+X] : Prv[n]; res.push_back(now); n = (n - now) / K; } reverse(res.begin(), res.end()); return res; } void Zero(){ for(int i=1; i<=N; i++){ if(A[i] % K != 0 || !DFS(-A[i] / K)) continue; vector<int> vec = Track(-A[i] / K); vec.push_back(A[i]); for(int j=0; j<vec.size(); j++) cout << vec[j] << " \n"[j+1==vec.size()]; return; } cout << "IMPOSSIBLE\n"; } 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(n == 0){ Zero(); continue; } 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 (stderr)

Main.cpp: In function 'void Zero()':
Main.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         for(int j=0; j<vec.size(); j++) cout << vec[j] << " \n"[j+1==vec.size()];
      |                      ~^~~~~~~~~~~
Main.cpp:51:68: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         for(int j=0; j<vec.size(); j++) cout << vec[j] << " \n"[j+1==vec.size()];
      |                                                                 ~~~^~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |         for(int i=0; i<vec.size(); i++) cout << vec[i] << " \n"[i+1==vec.size()];
      |                      ~^~~~~~~~~~~
Main.cpp:67:68: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |         for(int i=0; i<vec.size(); i++) cout << vec[i] << " \n"[i+1==vec.size()];
      |                                                                 ~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...