Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <vector>
  5. #define ll long long
  6. using namespace std;
  7. typedef vector<int> lll;
  8. const long long e=10, k=1;
  9.  
  10. istream& operator >>(istream& stream, lll& a) {
  11. string s, r;
  12. stream>>s;
  13. for (int i=s.size(); i>0; i-=k) {
  14. int b, j=i-k;
  15. if (j>=0) r=s.substr(j, k);
  16. else r=s.substr(0, i);
  17. b=stoi(r);
  18. a.push_back(b);
  19. }
  20. return stream;
  21. }
  22.  
  23. ostream& operator <<(ostream& stream, lll a) {
  24. stream<<a[a.size()-1];
  25. for (int i=a.size()-2; i>=0; i--)
  26. stream<<setw(k)<<setfill('0')<<a[i];
  27. return stream;
  28. }
  29.  
  30. bool operator >(lll a, lll b) {
  31. if (a.size()>b.size()) return true;
  32. if (a.size()<b.size()) return false;
  33. for (int i=a.size()-1; i>=0; i--) {
  34. if (a[i]>b[i]) return true;
  35. if (a[i]<b[i]) return false;
  36. }
  37. return false;
  38. }
  39.  
  40. int complong(lll &a, lll &b) {
  41. if (a.size()>b.size()) return 1;
  42. if (a.size()<b.size()) return -1;
  43. for (int i=a.size()-1; i>=0; i--) {
  44. if (a[i]>b[i]) return 1;
  45. if (a[i]<b[i]) return -1;
  46. }
  47. return 0;
  48. }
  49.  
  50. lll operator +(lll a, lll b) {
  51. lll c;
  52. int i, r=0;
  53. //if (complong(a, b)>0) swap(a, b);
  54. for (i=0; i<a.size()||i<b.size(); i++) {
  55. if (i<a.size()) r+=a[i];
  56. if (i<b.size()) r+=b[i];
  57. c.push_back(r%e);
  58. r/=e;
  59. }
  60. if (r) c.push_back(r);
  61. return c;
  62. }
  63.  
  64. lll operator -(lll a, lll b) {
  65. lll c;
  66. int i, r=0;
  67. if (complong(a, b)<0) swap(a, b);
  68. for (i=0; i<a.size(); i++) {
  69. //if (i<a.size())
  70. r+=a[i];
  71. if (i<b.size()) r-=b[i];
  72. if (r<0) {
  73. c.push_back(r+e);
  74. r=-1;
  75. }
  76. else {
  77. c.push_back(r);
  78. r=0;
  79. }
  80. }
  81. while (c.back()==0&&c.size()>1) c.pop_back();
  82. return c;
  83. }
  84.  
  85. lll operator *(lll a, ll b) {
  86. lll c; int i; ll r=0;
  87. if (b==0) {
  88. c.push_back(0);
  89. return c;
  90. }
  91. for (i=0; i<a.size()||r; i++) {
  92. if (i<a.size()) r+=a[i]*b;
  93. c.push_back(r%e);
  94. r/=e;
  95. }
  96. return c;
  97. }
  98.  
  99. lll operator *(lll a, lll b) {
  100. lll c{1, 0}, d; int i, j;
  101. if (a==d||b==d) return d;
  102. if (b.size()>a.size()) swap(a, b);
  103. for (i=0; i<b.size(); i++) {
  104. c=a*b[i];
  105. for (j=0; j<i; j++) c.insert(c.begin(), 0);
  106. d=d+c;
  107. }
  108. return d;
  109. }
  110.  
  111. lll operator /(lll a, ll b) {
  112. lll c;
  113. int i;
  114. ll r=0, t;
  115. for (i=a.size()-1; i>=0; i--) {
  116. r=r*e+a[i];
  117. t=r/b;
  118. c.insert(c.begin(), t);
  119. r-=t*b;
  120. }
  121. while (c.size()>1&&c[c.size()-1]==0)
  122. c.resize(c.size()-1);
  123. return c;
  124. }
  125.  
  126. ll operator %(lll a, ll b) {
  127. lll c;
  128. int i;
  129. ll r=0, t;
  130. for (i=a.size()-1; i>=0; i--) {
  131. r=r*e+a[i];
  132. t=r/b;
  133. r-=t*b;
  134. }
  135. return r;
  136. }
  137.  
  138. int main() {
  139. lll a={0}, z={0}, mp;
  140. vector<int> v;
  141. string s;
  142. ll i, k, m, n;
  143. char ch;
  144. cin>>m>>n>>s;
  145. if (s=="0") {
  146. cout<<0;
  147. return 0;
  148. }
  149. for (i=0; i<s.size(); i++) {
  150. a=a*m;
  151. ch=s[i];
  152. if (ch>='0'&&ch<='9') k=ch-'0';
  153. else if (ch>='A'&&ch<='Z') k=ch-'A'+10;
  154. mp={k};
  155. a=a+mp;
  156. }
  157. while (a>z) {
  158. v.push_back(a%n);
  159. a=a/n;
  160. }
  161. for (i=v.size()-1; i>=0; i--) {
  162. k=v[i];
  163. ch='A'+k-10;
  164. if (k<=9) cout<<k;
  165. else cout<<ch;
  166. }
  167. return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement