fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. #define sz(x) (int((x).size()))
  7. typedef vector<int> vi;
  8. typedef long long llong;
  9.  
  10. int DigToNumber(char c) {
  11. if( c <= '9' && c >= '0' )
  12. return c-'0';
  13. return c-'A'+10;
  14. }
  15. char NumberToDig(int n) {
  16. if( n < 10 )
  17. return '0'+n;
  18. return n-10+'A';
  19. }
  20.  
  21. const int base = 1000*1000*1000;
  22.  
  23. void mulint(vi& a, int b) {
  24. for(int i = 0, carry = 0; i < sz(a) || carry; i++) {
  25. if( i == sz(a) )
  26. a.push_back(0);
  27. llong cur = carry + a[i] * 1LL * b;
  28. a[i] = int(cur%base);
  29. carry = int(cur/base);
  30. }
  31. while( sz(a) > 1 && a.back() == 0 )
  32. a.pop_back();
  33. }
  34. int divint(vi& a, int d) {
  35. int carry = 0;
  36. for(int i = sz(a)-1; i >= 0; i--) {
  37. llong cur = a[i] + carry * 1LL * base;
  38. a[i] = int(cur/d);
  39. carry = int(cur%d);
  40. }
  41. while( sz(a) > 1 && a.back() == 0 )
  42. a.pop_back();
  43. return carry;
  44. }
  45. void add(vi& a, vi& b) {
  46. for(int i = 0, c = 0, l = max(sz(a),sz(b)); i < l || c; i++) {
  47. if( i == sz(a) )
  48. a.push_back(0);
  49. a[i] += ((i<sz(b))?b[i]:0) + c;
  50. c = a[i] >= base;
  51. if( c ) a[i] -= base;
  52. }
  53. }
  54.  
  55. int main() {
  56.  
  57. ios_base::sync_with_stdio(0);
  58. cin.tie(0);
  59.  
  60. int from, to; cin >> from >> to;
  61. string s; cin >> s;
  62. vi res(1,0); vi m(1,1); vi tmp;
  63. for(int i = sz(s)-1; i >= 0; i--) {
  64. tmp.assign(m.begin(), m.end());
  65. mulint(tmp,DigToNumber(s[i]));
  66. add(res,tmp); mulint(m,from);
  67. }
  68. vi ans;
  69. while( sz(res) > 1 || res.back() != 0 )
  70. ans.push_back(divint(res,to));
  71. if( sz(ans) == 0 )
  72. ans.push_back(0);
  73. for(int i = sz(ans)-1; i >= 0; i--)
  74. cout << NumberToDig(ans[i]);
  75. cout << "\n";
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0s 3480KB
stdin
10 2
555
stdout
1000101011