Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define f0r(a, b) for (int a = 0; a < (b); ++a)
- #define f0rd(a, b) for (int a = (b); a >= 0; --a)
- /* assuming MAXN = 2^18 - 1 = 262143, adjust MX to make it higher/lower */
- const int mx = 7, MX = 17;
- const int arr[7] = {137, 1315, 73, 136, 255, 1384, 16385};
- string rep(int x) {
- /* base cases */
- if (x == 0) return "0";
- if (x == 2) return "2";
- /* is x a power of 2 */
- f0rd(j, MX) {
- if (x == (1 << j)) {
- return "2(" + rep(j) + ")";
- }
- }
- /* any other form of number, iterate through powers of 2 in decreasing order */
- string res = "";
- f0rd(j, MX) {
- if (x & (1 << j)) {
- if (res.length() > 0) res += "+";
- res += rep(1 << j);
- }
- }
- return res;
- }
- int main() {
- f0r(i, mx) {
- cout << arr[i] << "=" << rep(arr[i]) << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement