fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int A(int n) {
  6. if (n == 1)
  7. return 0;
  8.  
  9. // k == 3^m
  10. int k = 1;
  11. while (3*k < n) k *= 3;
  12.  
  13. return A((n - 1) % k + 1) + ((n <= 2*k) ? k : 2*k);
  14. }
  15.  
  16. int
  17. main()
  18. {
  19. for (auto n: {1, 2, 3, 4, 5, 6, 9, 123457, 1000000000})
  20. cout << n << " " << A(n) << endl;
  21. }
  22.  
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
1 0
2 1
3 2
4 3
5 4
6 5
9 8
123457 123456
1000000000 999999999