Advertisement
Guest User

Untitled

a guest
May 5th, 2020
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define f0r(a, b) for (int a = 0; a < (b); ++a)
  6. #define f0rd(a, b) for (int a = (b); a >= 0; --a)
  7.  
  8. /* assuming MAXN = 2^18 - 1 = 262143, adjust MX to make it higher/lower */
  9. const int mx = 7, MX = 17;
  10. const int arr[7] = {137, 1315, 73, 136, 255, 1384, 16385};
  11.  
  12. string rep(int x) {
  13. /* base cases */
  14. if (x == 0) return "0";
  15. if (x == 2) return "2";
  16.  
  17. /* is x a power of 2 */
  18. f0rd(j, MX) {
  19. if (x == (1 << j)) {
  20. return "2(" + rep(j) + ")";
  21. }
  22. }
  23.  
  24. /* any other form of number, iterate through powers of 2 in decreasing order */
  25. string res = "";
  26. f0rd(j, MX) {
  27. if (x & (1 << j)) {
  28. if (res.length() > 0) res += "+";
  29. res += rep(1 << j);
  30. }
  31. }
  32. return res;
  33. }
  34.  
  35. int main() {
  36. f0r(i, mx) {
  37. cout << arr[i] << "=" << rep(arr[i]) << '\n';
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement