Skip to content

Instantly share code, notes, and snippets.

@bruceoutdoors
Last active March 21, 2017 13:51
Show Gist options
  • Save bruceoutdoors/6dbfaf44d6468627cd592229aa3b2fac to your computer and use it in GitHub Desktop.
Save bruceoutdoors/6dbfaf44d6468627cd592229aa3b2fac to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> ivec;
ivec polynomialMultiply(const ivec &a, const ivec &b)
{
int N = 2 * a.size() - 1;
ivec c(N, 0);
for (int j = 0; j < N; ++j) {
int p = j >= a.size() ? j - a.size() + 1 : 0;
for (int k = p; k <= j - p; ++k) {
c[j] += a[k] * b[j - k];
}
}
return c;
}
int main()
{
ivec a = { 6, 7, -10, 9 };
ivec b = { -2, 0, 4, -5 };
ivec c = polynomialMultiply(a, b);
for (const auto &t : c) cout << t << ' ';
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment