Submission #1103695

# Submission time Handle Problem Language Result Execution time Memory
1103695 2024-10-21T14:34:31 Z BuzzyBeez Weird Numeral System (CCO21_day1problem2) C++17
0 / 25
1 ms 336 KB
#include <bits/stdc++.h>
using namespace std;
// #define int long long

const long long inf = LLONG_MAX;

map<long long, long long> memo;
vector<int> arr;

long long k;
// bool attempt(long long R) {
	// if (par.count(R)) return (par[R] != inf);
	// par[R] = inf;
	// for (int x : arr) if (abs(R - x) % K == 0) 
		// if (R == x || attempt((R - x) / K)) {par[R] = x; return 1;}
	// return 0;
// }

bool solve(long long a){
    if(memo.count(a))return memo[a]!=LLONG_MAX;
    memo[a]=LLONG_MAX;
    for(auto x:arr){
        if((a-x)%k==0){
            if(a-x==0||solve((a-x)/k)){
                memo[a]=x;
                return true;
            }
        }
    }
    return false;
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	// int d, q, m; cin >> k >> q >> d >> m;
	// arr.resize(d);
	// for (int i = 0; i < d; ++i) cin >> arr[i];
	// long long N;
	 int q,d,m;
    cin>>k>>q>>d>>m;
    arr.resize(d);
    for(int i=0;i<d;i++)cin>>arr[i];
    long long N;
	while (q--) {
		cin >> N; bool pass = solve(N);
		if (!pass) cout << "IMPOSSIBLE\n";
		else {
			vector<long long> fin; long long cur = N;
            while(1){
                fin.push_back(memo[cur]);
                cur=(cur-memo[cur])/k;
                if(cur==0)break;
            }
            reverse(fin.begin(),fin.end());
			for (auto i : fin) cout << i << ' ';
			cout << '\n';
			// vector<long long> ans; long long x;
			// while (1) {
				// x = memo[N]; ans.push_back(x);
				// N = (N - x) / K;
				// if (N == 0) break;
			// }
			// reverse(ans.begin(), ans.end());
			// for (int i : ans) cout << i << ' ';
			// cout << '\n';
		}
	}
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB Expected integer, but "IMPOSSIBLE" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB Expected integer, but "IMPOSSIBLE" found
2 Halted 0 ms 0 KB -