给定由两个数组表示的两个多项式,编写一个函数,将给定的两个多项式相乘。
例子:
Input: A[] = {5, 0, 10, 6}
B[] = {1, 2, 4}
Output: prod[] = {5, 10, 30, 26, 52, 24}
The first input array represents "5 + 0x^1 + 10x^2 + 6x^3"
The second array represents "1 + 2x^1 + 4x^2"
And Output is "5 + 10x^1 + 30x^2 + 26x^3 + 52x^4 + 24x^5"
一个简单的解决方案是逐一考虑第一多项式的每一项并将其与第二多项式的每一项相乘。以下是这种简单方法的算法。
multiply(A[0..m-1], B[0..n01])
1) Create a product array prod[] of size m+n-1.
2) Initialize all entries in prod[] as 0.
3) Traverse array A[] and do following for every element A[i]
...(3.a) Traverse array B[] and do following for every element B[j]
prod[i+j] = prod[i+j] + A[i] * B[j]
4) Return prod[].
下面是上述算法的实现。
C++
// Simple C++ program to multiply two polynomials
#include
using namespace std;
// A[] represents coefficients of first polynomial
// B[] represents coefficients of second polynomial
// m and n are sizes of A[] and B[] respectively
int *multiply(int A[], int B[], int m, int n)
{
int *prod = new int[m+n-1];
// Initialize the porduct polynomial
for (int i = 0; i
Java
// Java program to multiply two polynomials
class GFG
{
// A[] represents coefficients
// of first polynomial
// B[] represents coefficients
// of second polynomial
// m and n are sizes of A[] and B[] respectively
static int[] multiply(int A[], int B[],
int m, int n)
{
int[] prod = new int[m + n - 1];
// Initialize the porduct polynomial
for (int i = 0; i < m + n - 1; i++)
{
prod[i] = 0;
}
// Multiply two polynomials term by term
// Take ever term of first polynomial
for (int i = 0; i < m; i++)
{
// Multiply the current term of first polynomial
// with every term of second polynomial.
for (int j = 0; j < n; j++)
{
prod[i + j] += A[i] * B[j];
}
}
return prod;
}
// A utility function to print a polynomial
static void printPoly(int poly[], int n)
{
for (int i = 0; i < n; i++)
{
System.out.print(poly[i]);
if (i != 0)
{
System.out.print("x^" + i);
}
if (i != n - 1)
{
System.out.print(" + ");
}
}
}
// Driver code
public static void main(String[] args)
{
// The following array represents
// polynomial 5 + 10x^2 + 6x^3
int A[] = {5, 0, 10, 6};
// The following array represents
// polynomial 1 + 2x + 4x^2
int B[] = {1, 2, 4};
int m = A.length;
int n = B.length;
System.out.println("First polynomial is n");
printPoly(A, m);
System.out.println("nSecond polynomial is n");
printPoly(B, n);
int[] prod = multiply(A, B, m, n);
System.out.println("nProduct polynomial is n");
printPoly(prod, m + n - 1);
}
}
// This code contributed by Rajput-Ji
Python3
# Simple Python3 program to multiply two polynomials
# A[] represents coefficients of first polynomial
# B[] represents coefficients of second polynomial
# m and n are sizes of A[] and B[] respectively
def multiply(A, B, m, n):
prod = [0] * (m + n - 1);
# Multiply two polynomials term by term
# Take ever term of first polynomial
for i in range(m):
# Multiply the current term of first
# polynomial with every term of
# second polynomial.
for j in range(n):
prod[i + j] += A[i] * B[j];
return prod;
# A utility function to print a polynomial
def printPoly(poly, n):
for i in range(n):
print(poly[i], end = "");
if (i != 0):
print("x^", i, end = "");
if (i != n - 1):
print(" + ", end = "");
# Driver Code
# The following array represents
# polynomial 5 + 10x^2 + 6x^3
A = [5, 0, 10, 6];
# The following array represents
# polynomial 1 + 2x + 4x^2
B = [1, 2, 4];
m = len(A);
n = len(B);
print("First polynomial is ");
printPoly(A, m);
print("\nSecond polynomial is ");
printPoly(B, n);
prod = multiply(A, B, m, n);
print("\nProduct polynomial is ");
printPoly(prod, m+n-1);
# This code is contributed by chandan_jnu
C#
// C# program to multiply two polynomials
using System;
class GFG
{
// A[] represents coefficients
// of first polynomial
// B[] represents coefficients
// of second polynomial
// m and n are sizes of A[]
// and B[] respectively
static int[] multiply(int []A, int []B,
int m, int n)
{
int[] prod = new int[m + n - 1];
// Initialize the porduct polynomial
for (int i = 0; i < m + n - 1; i++)
{
prod[i] = 0;
}
// Multiply two polynomials term by term
// Take ever term of first polynomial
for (int i = 0; i < m; i++)
{
// Multiply the current term of first polynomial
// with every term of second polynomial.
for (int j = 0; j < n; j++)
{
prod[i + j] += A[i] * B[j];
}
}
return prod;
}
// A utility function to print a polynomial
static void printPoly(int []poly, int n)
{
for (int i = 0; i < n; i++) {
Console.Write(poly[i]);
if (i != 0) {
Console.Write("x^" + i);
}
if (i != n - 1) {
Console.Write(" + ");
}
}
}
// Driver code
public static void Main(String[] args)
{
// The following array represents
// polynomial 5 + 10x^2 + 6x^3
int []A = {5, 0, 10, 6};
// The following array represents
// polynomial 1 + 2x + 4x^2
int []B = {1, 2, 4};
int m = A.Length;
int n = B.Length;
Console.WriteLine("First polynomial is n");
printPoly(A, m);
Console.WriteLine("nSecond polynomial is n");
printPoly(B, n);
int[] prod = multiply(A, B, m, n);
Console.WriteLine("nProduct polynomial is n");
printPoly(prod, m + n - 1);
}
}
// This code has been contributed by 29AjayKumar
PHP
Javascript
输出:
First polynomial is
5 + 0x^1 + 10x^2 + 6x^3
Second polynomial is
1 + 2x^1 + 4x^2
Product polynomial is
5 + 10x^1 + 30x^2 + 26x^3 + 52x^4 + 24x^5
上述解决方案的时间复杂度为 O(mn)。如果两个多项式的大小相同,则时间复杂度为 O(n 2 )。
我们能做得更好吗?
有一些方法可以比 O(n 2 ) 时间更快地进行乘法。这些方法主要基于分而治之。以下是一种简单的方法,可将给定的多项式(n 次)分成两个多项式,一个包含低次项(低于 n/2),另一个包含高次项(高于或等于 n/2)
Let the two given polynomials be A and B.
For simplicity, Let us assume that the given two polynomials are of
same degree and have degree in powers of 2, i.e., n = 2i
The polynomial 'A' can be written as A0 + A1*xn/2
The polynomial 'B' can be written as B0 + B1*xn/2
For example 1 + 10x + 6x2 - 4x3 + 5x4 can be
written as (1 + 10x) + (6 - 4x + 5x2)*x2
A * B = (A0 + A1*xn/2) * (B0 + B1*xn/2)
= A0*B0 + A0*B1*xn/2 + A1*B0*xn/2 + A1*B1*xn
= A0*B0 + (A0*B1 + A1*B0)xn/2 + A1*B1*xn
所以上面的分而治之的方法需要 4 次乘法和 O(n) 时间来添加所有 4 个结果。因此时间复杂度为 T(n) = 4T(n/2) + O(n)。递归的解是 O(n 2 ),与上面的简单解相同。
这个想法是将乘法次数减少到 3 并使递归为 T(n) = 3T(n/2) + O(n)
如何减少乘法次数?
这需要一个类似于施特拉森矩阵乘法的小技巧。我们做以下 3 次乘法。
X = (A0 + A1)*(B0 + B1) // First Multiplication
Y = A0B0 // Second
Z = A1B1 // Third
The missing middle term in above multiplication equation A0*B0 + (A0*B1 +
A1*B0)xn/2 + A1*B1*xn can obtained using below.
A0B1 + A1B0 = X - Y - Z
深入解释
常规多项式乘法使用 4 个系数乘法:
(ax + b)(cx + d) = acx2 + (ad + bc)x + bd
但是,请注意以下关系:
(a + b)(c + d) = ad + bc + ac + bd
其余两个分量恰好是两个多项式乘积的中间系数。因此,乘积可以计算为:
(ax + b)(cx + d) = acx2 +
((a + b)(c + d) - ac - bd )x + bd
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。