fork(1) download
  1. #include <cmath>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. typedef long long ll;
  9.  
  10. void init(ll x, vector<int> &A){
  11. int curIndex = 0;
  12. int curPower = 1;
  13. while(true){
  14. A[curIndex] = x % 3;
  15. x = (x-A[curIndex])/3;
  16. if (x == 0) break;
  17. curIndex++;
  18. }
  19. }
  20.  
  21. int main(int argc, char const *argv[])
  22. {
  23. vector<int> A(21, 0);
  24. vector<int> B(21, 0);
  25. int n;
  26. ll x;
  27. cin >> n >> x;
  28. //check feasibility
  29. if (x > static_cast<ll>((pow(3, n)-1)/2)) {
  30. cout << -1 << endl;
  31. return 0;
  32. }
  33. //initialize value of A with digits of x in base-3 representation
  34. init(x, A);
  35. for(int i = 0; i < 21; i++){
  36. if (A[i] == 0){
  37. //nothing
  38. continue;
  39. }
  40. if (A[i] == 1){
  41. A[i] = 0;
  42. B[i] = 1;
  43. continue;
  44. }
  45. if (A[i] == 2){
  46. A[i] = 1;
  47. A[i+1]++;
  48. continue;
  49. }
  50. if (A[i] == 3){
  51. A[i+1]++;
  52. continue;
  53. }
  54. }
  55. int i;
  56. i = 1;
  57. for(auto it: A){
  58. if (it == 1) cout << i << " ";
  59. i++;
  60. }
  61. cout << endl;
  62. i = 1;
  63. for(auto it: B){
  64. if (it == 1) cout << i << " ";
  65. i++;
  66. }
  67. cout << endl;
  68. return 0;
  69. }
  70.  
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
-1