kzhang01 / karatsuba

Karatsuba's fast multiplication algorithm implemented in Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Karatsuba's Algorithm

Karatsuba is a fast multiplication algorithm using Divide and Conquer.

The Hackerrank prompt is as follows:

Professor Farnsworth has been working on designing his new crazy invention, the Smell-O-Scope. This device lets mankind smell the different odors of the celestial bodies, over vast distances. Unfortunately these smells are sometimes mixed up while reaching earth. The Professor wants to model each smell as a function of various properties of the body, specifically as polynomials. He is also modelling the mixing of the smells as multiplying the two polynomials. If he can figure out the mixture formula he is sure that he can get back the original smells.

Help the Professor work out the simulation so he can start smelling the universe.

Input Format

The first line of the input is n, the degree of the two following polynomials.

The second line of the input consists of n+1 space separated integers a0,...,an, where ai is the coefficient of x^i.

The third line consists of b0,...,bn, similar to above.

Constraints

n+1=2^k, 2<=k<=16

Output Format

Output the coefficients of the product of the two polynomials, space separated in a single line, starting with the coefficient of x^0.

Because the coefficients can be very large, output the coefficients modulo the number (10^9)+9 = 1000000009.

About

Karatsuba's fast multiplication algorithm implemented in Java


Languages

Language:Java 100.0%