fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. ll val(char c){
  6. return c-'0';
  7. }
  8. int main()
  9. {
  10. ll n;
  11. string k;
  12.  
  13. cin >> n >> k;
  14. ll pos = k.size()-1;
  15. ll mul = 1;
  16. ll res = 0;
  17. while(pos >= 0){
  18. ll cu = 0;
  19. ll mu = 1;
  20. for(int i=pos+1; i<k.size() && k[i] == '0'; i++){
  21. mul *= n;
  22. }
  23. while(pos >= 0 && val(k[pos])*mu+cu < n){
  24. cu = val(k[pos])*mu+cu;
  25. pos--;
  26. mu *= 10;
  27. }
  28. res += cu*mul;
  29. mul *= n;
  30. }
  31. cout << res << endl;
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 3472KB
stdin
17
2016
stdout
594