답안 #1102532

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1102532 2024-10-18T08:42:41 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

map<long long, pair<int, int>> dp;
int a[5008];

int d, K;
int attempt(long long R) {
	if (dp.count(R)) return dp[R].first;
	dp[R] = make_pair(1, 0);
	for (int i = 1; i <= d; ++i) if (R == a[i]) {dp[R] = make_pair(2, i); return 2;}
	for (int i = 1; i <= d; ++i) if (abs(R - a[i]) % K == 0) {
		if (attempt((R - a[i]) / K) == 2) {dp[R] = make_pair(2, i); return 2;}
	}
	return 1;
}

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];
	long long N; bool pass; 
	while (q--) {
		cin >> N; pass = 0; dp.clear();
		for (int i = 1; i <= d; ++i) if (attempt(N) == 2) {pass = 1; break;}
		if (!pass) cout << "IMPOSSIBLE\n";
		else {
			vector<int> ans; int x;
			if (N == 0) {ans.push_back(a[dp[0].second]); N = a[dp[0].second];}
			while (N != 0) {
				x = a[dp[N].second]; ans.push_back(x);
				N = (N - x) / K;
			}
			reverse(ans.begin(), ans.end());
			for (int i : ans) cout << i << ' ';
			cout << '\n';
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 336 KB Expected integer, but "IMPOSSIBLE" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 336 KB Expected integer, but "IMPOSSIBLE" found
2 Halted 0 ms 0 KB -