# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1096390 | slah007 | Weird Numeral System (CCO21_day1problem2) | C++17 | 2098 ms | 1640 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cassert>
#include <set>
#include <map>
using namespace std;
const int MAXK = 1e6;
const int MAXM = 2500;
const int MAXD = MAXM<<1|1;
const int NAN = 998244353;
int K, Q, D, M;
set<int> digits;
map<long long, int> dp;
int go(long long n){
if(dp.count(n)) return dp[n];
if(digits.count(n)){
return dp[n] = n;
}
dp[n] = NAN;
for(auto x : digits){
if((n-x)%K == 0){
long long t = (n-x)/K;
if(go(t) != NAN){
return dp[n] = x;
}
}
}
return NAN;
}
int main(){
scanf("%d %d %d %d", &K, &Q, &D, &M);
for(int i=0;i<D;i++){
int x;
scanf("%d", &x);
digits.insert(x);
}
while(Q--){
long long n;
scanf("%lld", &n);
dp.clear();
if(go(n) != NAN){
vector<int> v;
do{
v.push_back(dp[n]);
n = (n - dp[n]) / K;
}while(n);
reverse(v.begin(), v.end());
for(int i=0;i<(int)v.size();i++){
printf("%d%c", v[i], " \n"[i+1==(int)v.size()]);
}
}
else{
puts("IMPOSSIBLE");
}
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |