Submission #1103606

# Submission time Handle Problem Language Result Execution time Memory
1103606 2024-10-21T11:42:35 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 = 2e18;

map<long long, long long> par;
int a[5008];

int d, K;
bool attempt(long long R) {
	if (par.count(R)) return (par[R] != inf);
	par[R] = inf;
	for (int i = 1; i <= d; ++i) if (abs(R - a[i]) % K == 0) {
		if (attempt((R - a[i]) / K)) {par[R] = a[i]; return 1;}
	}
	return 0;
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q, m; cin >> K >> q >> d >> m;
	for (int i = 1; i <= d; ++i) cin >> a[i], par[a[i]] = a[i];
	long long N, CN;
	while (q--) {
		cin >> N; CN = N; 
		if (!attempt(N)) cout << "IMPOSSIBLE\n";
		else {
			vector<int> ans; long long x;
			while (1) {
				x = par[N]; ans.push_back(x);
				N = (N - x) / K;
				if (N == 0) break;
			}
			for (int i : ans) CN = (CN - i) / K;
			assert(CN == 0);
			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 -