fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define el "\n"
  4. #define ll long long
  5. #define ull unsigned long long
  6. #define se second
  7. #define fi first
  8. #define be begin()
  9. #define en end()
  10. #define Faster cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
  11.  
  12. int main()
  13. {
  14. Faster;
  15. string a, b; cin >> a >> b;
  16. string s = "";
  17. int du = 0;
  18. if(a.size() == b.size())
  19. {
  20. for(int i = b.size() - 1; i >= 0; i--)
  21. {
  22. int x = a[i] - '0';
  23. int y = b[i] - '0';
  24. s += to_string((x + y + du) % 10);
  25. du = (x + y + du) / 10;
  26. }
  27. }
  28. else
  29. {
  30. string tmp = "";
  31. for(int i = 0; i < a.size() - b.size(); i++)
  32. {
  33. tmp += '0';
  34. }
  35. if(a.size() < b.size()) a = tmp + a;
  36. else b = tmp + b;
  37. for(int i = b.size() - 1; i >= 0; i--)
  38. {
  39. int x = a[i] - '0';
  40. int y = b[i] - '0';
  41. s += to_string((x + y + du) % 10);
  42. du = (x + y + du) / 10;
  43. }
  44. }
  45. if(du != 0) s += to_string(du);
  46. reverse(s.begin(), s.end());
  47. cout << s;
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0.01s 5276KB
stdin
9999999999999
22
stdout
10000000000021