LASCALE - Quả Cân

Tác giả: happyboy99x

Ngôn ngữ: C++

#include<bits/stdc++.h>
using namespace std;

const int P = 37, MAX = 500, INF = 1e9;
long long pow3[P+1];
int need[P+1];

void init() {
	pow3[0] = 1;
	for(int i = 1; i <= P; ++i)
		pow3[i] = pow3[i-1] * 3;
}

void print(const vector<int> &a) {
	cout << a.size();
	for(unsigned i = 0; i < a.size(); ++i)
		cout << ' ' << a[i];
	cout << '\n';
}

void scale(int v) {
	while(v != 0) {
		int x = upper_bound(pow3, pow3+P+1, v) - pow3 - 1;
		++need[x]; v -= pow3[x];
	}
	vector<int> res[2];
	for(int i = 0; i <= P; ++i) {
		while(i < P && need[i] > 1)
			++need[i+1], need[i] -= 3;
		if(need[i] < 0) res[0].push_back(pow3[i]);
		else if(need[i] > 0) res[1].push_back(pow3[i]);
	}
	print(res[0]); print(res[1]);
}

int main() {
	ios::sync_with_stdio(false);
	init();
	int m; cin >> m;
	scale(m);
	return 0;
}

Download