Submission #507653

#TimeUsernameProblemLanguageResultExecution timeMemory
507653ETKWeird Numeral System (CCO21_day1problem2)C++14
0 / 25
1 ms204 KiB
#include <bits/stdc++.h> #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define per(i,a,b) for(int i=(a);i>=(b);--i) #define pii pair<int,int> #define vi vector<int> #define fi first #define se second #define pb push_back #define debug(...) fprintf(stderr, __VA_ARGS__) #define ALL(x) x.begin(),x.end() #define sz(x) int(x.size()) #define ll long long using namespace std; inline ll read(){ ll x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();} while (isdigit(ch)){x=x*10+ch-48;ch=getchar();} return x*f; } const int N=5005; int K,q,D,M,a[N]; unordered_map <ll,bool> mp; vector <int> ans; bool dp(ll x){ if(mp.find(x) != mp.end())return mp[x]; mp[x] = 0; rep(i,1,D){ if(x == a[i]){ ans.pb(a[i]); return mp[x] = 1; } if((x-a[i]) % K == 0){ if( (x-a[i]) / K != x && dp((x - a[i]) / K)){ ans.pb(a[i]); return mp[x] = 1; } } } return mp[x]; } int main(){ K = read(),q = read(),D = read(),M = read(); rep(i,1,D){ a[i] = read(); } while(q--){ ll x = read(); ans.clear(); mp.clear(); if(dp(x)){ rep(i,0,sz(ans)-1)printf("%d ",ans[i]); puts(""); }else puts("IMPOSSIBLE"); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...