Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- long multiply(long x, long y, int BASE = 10)
- {
- const int xLength = to_string(x).length();
- const int yLength = to_string(y).length();
- int maxi = max(xLength, yLength);
- if (maxi < 10)
- return (long)x * (long)y;
- int N = maxi / 2;
- long multiplier = (long)pow(BASE, N);
- long a = x / multiplier;
- long b = x % multiplier;
- long c = y / multiplier;
- long d = y % multiplier;
- long ac = multiply(a, c, BASE);
- long bd = multiply(b, d, BASE);
- long res = multiply((a + b), (c + d), BASE);
- long result = ac * (long)pow(BASE, N) + bd + ((long)pow(BASE, N / 2) * res);
- return result;
- }
- int main()
- {
- long x = 134, y = 2343434;
- cout << x << " times " << y << " = " << multiply(x, y) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment