Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#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