답안 #1103706

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1103706 2024-10-21T14:44:57 Z vjudge1 Weird Numeral System (CCO21_day1problem2) C++17
25 / 25
1919 ms 6372 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;
			}
			reverse(ans.begin(), ans.end());
			for (int i = 0; i + 1 < ans.size(); ++i) cout << ans[i] << ' ';
			cout << ans.back() << '\n';
		}
	}
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:37:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |    for (int i = 0; i + 1 < ans.size(); ++i) cout << ans[i] << ' ';
      |                    ~~~~~~^~~~~~~~~~~~
Main.cpp:25:15: warning: variable 'CN' set but not used [-Wunused-but-set-variable]
   25 |  long long N, CN;
      |               ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB OK
2 Correct 1 ms 336 KB OK
3 Correct 1 ms 336 KB OK
4 Correct 1 ms 336 KB OK
5 Correct 1 ms 336 KB OK
6 Correct 1 ms 336 KB OK
7 Correct 1 ms 336 KB OK
8 Correct 1 ms 336 KB OK
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB OK
2 Correct 1 ms 336 KB OK
3 Correct 1 ms 336 KB OK
4 Correct 1 ms 336 KB OK
5 Correct 1 ms 336 KB OK
6 Correct 1 ms 336 KB OK
7 Correct 1 ms 336 KB OK
8 Correct 1 ms 336 KB OK
9 Correct 1 ms 504 KB OK
10 Correct 1 ms 336 KB OK
11 Correct 1 ms 336 KB OK
12 Correct 1 ms 336 KB OK
13 Correct 1 ms 336 KB OK
14 Correct 1 ms 336 KB OK
15 Correct 1 ms 336 KB OK
16 Correct 1 ms 336 KB OK
17 Correct 1 ms 336 KB OK
18 Correct 1 ms 336 KB OK
19 Correct 1 ms 336 KB OK
20 Correct 1 ms 336 KB OK
21 Correct 60 ms 1612 KB OK
22 Correct 397 ms 1580 KB OK
23 Correct 1919 ms 6372 KB OK
24 Correct 769 ms 2764 KB OK
25 Correct 1 ms 336 KB OK
26 Correct 1 ms 336 KB OK
27 Correct 1 ms 504 KB OK
28 Correct 0 ms 336 KB OK