CJamie

kasturba

May 12th, 2022
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. long multiply(long x, long y, int BASE = 10)
  6. {
  7. const int xLength = to_string(x).length();
  8. const int yLength = to_string(y).length();
  9.  
  10. int maxi = max(xLength, yLength);
  11. if (maxi < 10)
  12. return (long)x * (long)y;
  13. int N = maxi / 2;
  14. long multiplier = (long)pow(BASE, N);
  15. long a = x / multiplier;
  16. long b = x % multiplier;
  17. long c = y / multiplier;
  18. long d = y % multiplier;
  19.  
  20. long ac = multiply(a, c, BASE);
  21. long bd = multiply(b, d, BASE);
  22. long res = multiply((a + b), (c + d), BASE);
  23.  
  24. long result = ac * (long)pow(BASE, N) + bd + ((long)pow(BASE, N / 2) * res);
  25. return result;
  26. }
  27.  
  28. int main()
  29. {
  30. long x = 134, y = 2343434;
  31.  
  32. cout << x << " times " << y << " = " << multiply(x, y) << endl;
  33.  
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment