Basic version of Polynomial Multiplication

Source: Internet
Author: User
Tags acos cos

Fast Fourier TransformationIn the informatics competition, it is mainly usedConvolution, OrPolynomial Multiplication. We know that the time complexity of common polynomial multiplication algorithms

Yes, the time can be reduced by using the fast Fourier transformation, so we will introduce in detail nextFast Fourier Transformation.

 

First, we will introduce two polynomial representation methods, namelyCoefficient representationAndPoint-value representation. In a sense, these two methods are equivalent. First set

 

 

 

(1) coefficient representation

 

For a polynomial in the number field, its coefficient representation is a vector composed of coefficients.

Obviously, the time complexity of such polynomial multiplication is.

 

(2) point value representation

 

The vertex value of a polynomial in the number field is a set of vertex value pairs.

 

 

They are different, and at that time, there are. We can see that a polynomial can have multiple different vertex values.

The point value pair can represent a unique polynomial. The point-value representation is used to calculate the multiplication and time of polynomials.

The complexity is.

 

In principle, it is easy to calculate the point value of a polynomial, because we only need to select a different point and thenQinjiu AlgorithmYes

In order to find all the values in time, in fact, if we choose to be clever, we can accelerate this process and change its running time

Yes.

 

The process of finding the point value representation based on the coefficient representation of polynomial is calledEvaluateThe process of finding the coefficient representation based on the dot-value notation is calledInterpolation.

 

ForConvolutionOrPolynomial MultiplicationComputing problems, first throughFourier transformEvaluate the polynomial of the coefficient representation.

The complexity is, and then the point value is multiplied in time, and then the interpolation operation is performed.

 

Next we will focus on how to calculate a polynomial efficiently, that is, converting the polynomial notation into the dot-value notation.

 

If you selectUnit Compound RootAs the evaluation point, you can performDiscrete Fourier Transform (DFT)To obtain the corresponding vertex value. Similarly

You can also use point-value pairsInverse DFTTo obtain the corresponding coefficient vector.DFTAndInverse DFTThe time complexity is.

 

 

1. Evaluate DFT

 

Selecting unit compound root as the point value is a clever practice.

The Unit Compound Root is the plural value that is satisfied, and the unit compound root has exactly one. They are

Using the definition of the plural power, the value is called the Primary and Secondary unit root.

The Unit Compound Root is the power of the second.

 

The Unit Compound Root forms a group under the multiplication operation. The group structure is the same as that of the addition group model.

 

Next, let's take a look at several important features of unit root.

(1) elimination Theorem

 

For any integer

 

(2) Half-Outsourcing

 

If the value is an even number

 

(3) summation Theorem

 

For any integer and non-zero integer that cannot be divisible, there are

 

 

Looking back, we want to calculate the polynomial of the number of bounds.

 

 

Where the value is assumed to be2Because the total number of times can be increased. If needed, the total number can be added to zero.

. Assume that the known form of coefficients is, right, and the result is defined.

As follows:

 

 

The vector is the Discrete Fourier transformation and writing of the coefficient vector.

By using a Fast Fourier Transform(FFT)And can be calculated in time, directly

The time required for the calculation method is,FFTIt mainly utilizes the special properties of unit Compound Root.FFTThe sub-governance policy is used.

The coefficients of the even number and the odd number respectively define two new number bounded polynomials and

 

 

Then

 

In this way, the worth problem at the place is transformed into the polynomial and the point where the frequency field is located.

. Because the order changes during the parity classification, you must first passBytesAlgorithm

In reverse orderFFTThe most important operationButterfly operation, ThroughButterfly operationYou can obtain the values of the first half and the second half.

 

Question: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1402

 

Question:Use the multiplication of large numbersFFT.

 

Code:

# Include <iostream> # include <string. h> # include <stdio. h> # include <math. h> using namespace STD; const int n = 500005; const double Pi = ACOs (-1.0); struct virt {Double R, I; virt (double r = 0.0, double I = 0.0) {This-> r = r; this-> I = I;} virt operator + (const virt & X) {return virt (R + X. r, I + X. i);} virt operator-(const virt & X) {return virt (R-X. r, I-X. i);} virt operator * (const virt & X) {Return virt (R * X. r-I * X. i, I * X. R + R * X. i) ;}}; // redright algorithm -- void reverse (virt f [], int Len) {Int J = Len> 1; for (INT I = 1; I <len-1; I ++) {if (I <j) Swap (F [I], F [J]); int K = Len> 1; while (j> = k) {J-= K; k> = 1 ;}if (j <k) J + = K ;}} // implement void FFT (virt f [], int Len, int on) {trim (F, Len); For (int h = 2; H <= Len; h <= 1) // After partitioning, calculate the DFT {virt Wn (COS (-on * 2 * PI/h) with the length of H ), sin (-on * 2 * PI/H); // unit Compound Root E ^ (2 * PI/m) expanded for (Int J = 0; j <Len; j + = h) {virt W () Using Euler's formula ); // rotation factor for (int K = J; k <j + H/2; k ++) {virt u = f [k]; virt T = W * f [K + H/2]; F [k] = u + T; // butterfly merge operation f [K + H/2] = u-t; W = W * wn; // update the rotation factor }}} if (on =-1) for (INT I = 0; I <Len; I ++) f [I]. r/= Len;} // perform convolution void Conv (virt A [], virt B [], int Len) {FFT (A, Len, 1); FFT (B, len, 1); For (INT I = 0; I <Len; I ++) A [I] = A [I] * B [I]; FFT (, len,-1);} Char str1 [N], str2 [N]; virt va [N], VB [N]; int result [N]; int Len; void Init (char str1 [], char str2 []) {int len1 = strlen (str1); int len2 = strlen (str2); Len = 1; while (LEN <2 * len1 | Len <2 * len2) Len <= 1; int I; for (I = 0; I <len1; I ++) {va [I]. R = str1 [len1-i-1]-'0'; VA [I]. I = 0.0;} while (I <Len) {va [I]. R = va [I]. I = 0.0; I ++;} for (I = 0; I <len2; I ++) {VB [I]. R = str2 [len2-i-1]-'0'; Vb [I]. I = 0.0;} while (I <Len) {VB [I]. R = VB [I]. I = 0.0; I ++ ;}} void work () {Conv (va, VB, Len); For (INT I = 0; I <Len; I ++) result [I] = va [I]. R + 0.5;} void Export () {for (INT I = 0; I <Len; I ++) {result [I + 1] + = Result [I]/10; Result [I] % = 10;} int high = 0; For (INT I = len-1; i> = 0; I --) {If (result [I]) {high = I; break; }}for (INT I = high; I> = 0; I --) printf ("% d", result [I]); puts ("") ;}int main () {While (~ Scanf ("% S % s", str1, str2) {Init (str1, str2); Work (); export () ;}return 0 ;}


Question:Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4609

 

Question:Given n known edges, how many triangles can be formed.

 

Analysis:Use a num array to record the number of times. For example, num [I] indicates that the edge with the length of I contains num [I. Then perform convolution on num [] to remove its own weight.

Repeat and symmetric, and then sort it out.

 

Code:

# Include <iostream> # include <string. h> # include <algorithm> # include <stdio. h> # include <math. h> using namespace STD; typedef long ll; const int n = 400005; const double Pi = ACOs (-1.0); struct virt {Double R, I; virt (double r = 0.0, double I = 0.0) {This-> r = r; this-> I = I;} virt operator + (const virt & X) {return virt (R + X. r, I + X. i);} virt operator-(const virt & X) {return virt (r-x.r, i-x. I);} virt operator * (const virt & X) {return virt (R * X. r-I * X. i, I * X. R + R * X. i) ;}}; // redright algorithm -- void reverse (virt f [], int Len) {Int J = Len> 1; for (INT I = 1; I <len-1; I ++) {if (I <j) Swap (F [I], F [J]); int K = Len> 1; while (j> = k) {J-= K; k> = 1 ;}if (j <k) J + = K ;}} // implement void FFT (virt f [], int Len, int on) {trim (F, Len); For (int h = 2; H <= Len; h <= 1) // After partitioning, calculate the DFT {virt Wn (COS (-on * 2 * PI/h) with the length of H ), sin (-on * 2 * PI/H); // unit Compound Root e ^ (2 * PI/m) expanded by Euler's formula for (Int J = 0; j <Len; j + = h) {virt W (1, 0); // rotation factor for (int K = J; k <j + H/2; k ++) {virt u = f [k]; virt T = W * f [K + H/2]; F [k] = u + T; // butterfly merge operation f [K + H/2] = u-t; W = W * wn; // update the rotation factor }}} if (on =-1) for (INT I = 0; I <Len; I ++) f [I]. r/= Len;} // perform convolution void Conv (virt f [], int Len) {FFT (F, Len, 1); For (INT I = 0; I <Len; I ++) f [I] = f [I] * f [I]; FFT (F, Len,-1);} int A [n]; virt f [N]; ll num [N], sum [N]; int Len, N; void Init () {memset (Num, 0, sizeof (Num )); scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% d ", & A [I]); num [A [I] ++;} Sort (A, A + n); int len1 = A [n-1] + 1; len = 1; while (LEN <len1 * 2) Len <= 1; for (INT I = 0; I <len1; I ++) f [I] = virt (Num [I], 0); For (INT I = len1; I <Len; I ++) f [I] = virt (0, 0);} void work () {Conv (F, Len); For (INT I = 0; I <Len; I ++) num [I] = (LL) (F [I]. R + 0.5); Len = A [n-1] * 2; for (INT I = 0; I <n; I ++) num [A [I] + A [I] --; For (INT I = 1; I <= Len; I ++) num [I]> = 1; sum [0] = 0; For (INT I = 1; I <= Len; I ++) sum [I] = sum [I-1] + num [I]; ll CNT = 0; For (INT I = 0; I <n; I ++) {CNT + = sum [Len]-sum [A [I]; // subtract a get big, a get small CNT-= (LL) (n-1-i) * I; // subtract a get itself, the other one takes the other CNT-= (n-1); // subtract the combination of the two greater than it CNT-= (LL) (n-1-i) * (n-i-2)/2 ;} ll tot = (LL) N * (n-1) * (n-2)/6; printf ("%. 7lf \ n ", (double) CNT/ToT);} int main () {int t; scanf (" % d ", & T); While (t --) {Init (); Work ();} return 0 ;}

 

 

Basic version of Polynomial Multiplication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.