fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. void calculate_power(vector <int> &a, int x, int y) //caculate x^y and store the result in a
  6. {
  7. int copy = x;
  8. int m = 0;
  9. while(copy > 0)
  10. {
  11. a.push_back(copy % 10);
  12. copy = copy/10;
  13. m++;
  14. }
  15. long long carry=0;
  16.  
  17. for(int loop = 1; loop < y; loop++)
  18. {
  19. for(int j = 0; j < m; j++)
  20. {
  21. long long prod=a[j]*x + carry;
  22. a[j] = prod % 10;
  23. carry = prod / 10;
  24. }
  25. while(carry)
  26. {
  27. a.push_back(carry%10);
  28. carry = carry/10;
  29. m++;
  30. }
  31. }
  32. }
  33.  
  34. void subtract(vector <int> &a, vector <int> &b) // subtract the two array
  35. {
  36. vector <int> c;
  37. while(b.size() != a.size())
  38. b.push_back(0);
  39.  
  40. /*for(int i = 0; i < a.size(); i++)
  41. cout << a[a.size() - i - 1];
  42.   cout << endl;
  43.   for(int i = 0; i < b.size(); i++)
  44.   cout << b[b.size() - i - 1];
  45.   cout << endl;*/
  46.  
  47. for(int i = 0; i < a.size(); i++)
  48. {
  49. if(a[i] >= b[i])
  50. c.push_back(a[i] - b[i]);
  51. else
  52. {
  53. a[i] = a[i]+10;
  54. c.push_back(a[i] - b[i]);
  55. int temp = 1;
  56. while(true)
  57. {
  58. if(a[i + temp] >= 0 and (i + temp) < a.size())
  59. {
  60. a[i + temp]--;
  61. break;
  62. }
  63. else
  64. temp++;
  65. }
  66. }
  67. }
  68. int temp = c.size() - 1;
  69. while(c[temp] == 0 and temp >= 1)
  70. temp --;
  71. for(; temp >= 0; temp--)
  72. cout << c[temp];
  73. cout << endl;
  74. }
  75.  
  76. int main()
  77. {
  78.  
  79. ios_base :: sync_with_stdio(false);
  80. cin.tie(NULL);
  81. cout.tie(NULL);
  82. vector <int> a; // stores a^b
  83. vector <int> b; // stores b^a
  84. int x, y;
  85. cin >> x;
  86. cin >> y;
  87. calculate_power(a, x, y);
  88. calculate_power(b, y, x);
  89.  
  90. if(x == 0 and y == 0)
  91. {
  92. cout << 0 << endl;
  93. return 0;
  94. }
  95. else if(y == 0)
  96. {
  97. cout << "1" << endl;
  98. return 0;
  99. }
  100. else if(x == 0)
  101. {
  102. cout << "-1" << endl;
  103. return 0;
  104. }
  105.  
  106. // subtract array
  107.  
  108. if(a.size() < b.size())
  109. {
  110. cout << "-";
  111. subtract(b, a);
  112. }
  113. else if(a.size() == b.size())
  114. {
  115. int check = 1;
  116. for(int i = a.size() - 1; i >= 0; i--)
  117. if(a[i] > b[i])
  118. {
  119. check = 0;
  120. subtract(a, b);
  121. break;
  122. }
  123. else if(a[i] < b[i])
  124. {
  125. check = 0;
  126. cout << "-";
  127. subtract(b, a);
  128. break;
  129. }
  130. if(check)
  131. subtract(a, b);
  132. }
  133. else
  134. subtract(a, b);
  135.  
  136. return 0;
  137. }
  138.  
Success #stdin #stdout 0s 4436KB
stdin
45 64
stdout
6391471866594630806511972412966518678476139190296004939272965568306119748729115006965166559087764692179201