#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+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 ? 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
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 time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
OK |
2 |
Incorrect |
1 ms |
340 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 |
0 ms |
340 KB |
OK |
2 |
Incorrect |
1 ms |
340 KB |
Query 1: Jury has an answer but participant does not |
3 |
Halted |
0 ms |
0 KB |
- |