分治法實現大整數乘法【C++語言】
如果實現傳統演算法中兩個n位整數相乘,第一個整數中的n個數字都要分別乘以第二個整數的n個數字,這樣就一共要做n*n次乘法。看上去設計一個乘法次數少於n*n的演算法是不可能的,但事實證明並非如此,可以使用分治的思想計算兩個大整數的相乘。
首先從僅有兩位數字的兩個數12和34考慮,12 = 1 * 10 + 2,34 = 3 * 10 + 4
把它們相乘:408 = 12 * 34 = (1 * 3) * 100 + (1 * 4 + 2 * 3) * 10 + 2 * 4
上式雖然產生了正確的結果,但是也使用了4次乘法。但是中間項的計算結果可以使用已經計算過的1 * 3和2 * 4來簡化依次乘法
1 * 4 + 2 * 3 = (1 + 4) * ( 2 + 3) - 1 * 3 - 2 * 4
用更一般的字母代替上面的過程,可以得到如下的公式:
c = a * b = c2*100 + c1*10 + c0,其中c2 = a1 * b1, c0 = a0 * b0, c1 = a1 * b0 + a0 * b1 = (a1 + a0) * (b1 + b0) - (c2 +c0)
a的前半部分記作a1, 後半部分記作a0,b的前半部分記作b1, 後半部分記作b0
對於位數更高的n來說
c = a * b
= (a1 * 10 ^ n/2 + a0) * (b1 * 10 ^ n/2 + b0)
= (a1*b1)*10^n + (a1 * b0 + a0 * b1) * 10 ^ n/2 + (a0 * b0)
= c2 * 10 ^ n + c1 * 10 ^ n/2 + c0
如果不考慮大整數的要求,僅考慮int範圍內的整數相乘,則程式碼實現如下
#include <bits/stdc++.h> using namespace std; int qpow(int n, int a = 10) { int ret = 1; while (n) { if (n & 1) ret = ret * a; a = a * a; n >>= 1; } return ret; } int len(int n) { int a = 0; while (n) { n /= 10; ++a; } return a; } int bigmul(int a, int b, int n) { if (n <= 2) return a * b; int a1 = a / qpow(n / 2); int a0 = a % qpow(n / 2); int b1 = b / qpow(n / 2); int b0 = b % qpow(n / 2); int c2 = bigmul(a1, b1, n / 2); int c0 = bigmul(a0, b0, n / 2); int c1 = bigmul(a1 + a0, b1 + b0, n / 2) - (c2 + c0); return c2 * qpow(n) + c1 * qpow(n / 2) + c0; } int main() { int a, b, n; while (cin >> a >> b) { n = qpow(ceil(log2(max(len(a), len(b)))), 2); cout << bigmul(a, b, n) << endl; } }
如果進一步考慮大整數的要求,則可以通過字串模擬操作實現,具體程式碼如下:
#include <bits/stdc++.h>
using namespace std;
template <typename T>
int toInt(T s)
{
// 將字串或字元轉化為數字
int res;
stringstream ss;
ss << s;
ss >> res;
return res;
}
string toStr(int n)
{
// 將數字轉為字串
string res;
stringstream ss;
ss << n;
ss >> res;
return res;
}
void addZero(string &s, int n, bool pre = true)
{
// 在字串前或者字串後新增0(預設為前)
string temp(n, '0');
s = pre ? temp + s : s + temp;
}
void removeZero(string &s)
{
// 去除前導零
int i = 0;
while (i < s.length() && s[i] == '0')
++i;
if (i < s.length())
s = s.substr(i);
else
s = "0";
}
string add(string a, string b)
{
// 大數加法(只考慮a+b非負)
string res;
removeZero(a);
removeZero(b);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int l = max((int)a.size(), (int)b.size());
for (int i = 0, j = 0; j || i < l; ++i)
{
int t = j;
if (i < a.size())
t += toInt(a[i]);
if (i < b.size())
t += toInt(b[i]);
int q = t % 10;
res = char(q + '0') + res;
j = t / 10;
}
return res;
}
string sub(string a, string b)
{
// 大數減法(只考慮a-b非負)
string res;
removeZero(a);
removeZero(b);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int lx = (int)a.size(), ly = (int)b.size(), j = 0;
//int* temp = new int[lx];
int *temp = (int *)calloc(lx, sizeof(int));
for (int i = 0; i < lx; ++i)
{
int ai = toInt(a[i]);
int bi = i < ly ? toInt(b[i]) : 0;
temp[j++] = ai - bi;
}
for (int i = 0; i < lx; ++i)
{
if (temp[i] < 0)
{
temp[i] += 10;
--temp[i + 1];
}
}
for (int i = lx - 1; i >= 0; --i)
{
res += toStr(temp[i]);
}
return res;
}
string mul(string a, string b)
{
// 大數乘法(只考慮a,b非負)
string res;
int n = 2;
if (a.size() > 2 || b.size() > 2)
{
n = 4;
while (n < a.size() || n < b.size())
n <<= 1;
addZero(a, n - (int)a.size());
addZero(b, n - (int)b.size());
}
if (a.size() == 1)
addZero(a, 1);
if (b.size() == 1)
addZero(b, 1);
if (n == 2)
{ // 遞迴終止
int temp = toInt(a) * toInt(b);
res = toStr(temp);
}
else
{
string a1, a0, b1, b0, c2, c1, c0;
a1 = a.substr(0, n / 2);
a0 = a.substr(n / 2);
b1 = b.substr(0, n / 2);
b0 = b.substr(n / 2);
c2 = mul(a1, b1);
c0 = mul(a0, b0);
c1 = sub(mul(add(a0, a1), add(b0, b1)), add(c2, c0));
addZero(c2, n, false);
addZero(c1, n / 2, false);
res = add(add(c2, c1), c0);
}
return res;
}
int main()
{
string a, b;
while (cin >> a >> b)
{
cout << mul(a, b) << endl;
}
}