简体   繁体   中英

Determining if there exists numbers n1, n2 in a, b and n3 in c such that n1 + n2 = n3 [ftt, polynomial multiplication]

Hello I am working on a problem that seems to be out of my league so any tips, pointers to reading materials etc. are really appreciated. That being said here is the problem:

given 3 subsets of numbers a, b, c ⊆ {0, ..., n}. In nlog(n) check if there exists numbers n1, n2 in a, b and n3 in c where n1 + n2 = n3.

I am given the hint to convert a and b to polynomial coefficients and to use polynomial multiplication using ftt to multiply the coefficients of a and b.

Now where I am stuck is after getting the result of the polynomial multiplication, what do I do next?

Thank you in advanced.

from numpy.fft import fft, ifft
from numpy import real, imag

def polynomial_multiply(a_coeff_list, b_coeff_list):
    # Return the coefficient list of the multiplication 
    # of the two polynomials 
    # Returned list must be a list of floating point numbers.
    # list from complex to reals by using the 
    # real function in numpy
    len_a = len(a_coeff_list)
    len_b = len(b_coeff_list)
    for i in range(len_a-1):
        b_coeff_list.append(0)
    for i in range(len_b-1):
        a_coeff_list.append(0)
    a_fft = fft(a_coeff_list)
    b_fft = fft(b_coeff_list)
    c = []
    for i in range(len(a_fft)):
        c.append(a_fft[i] * b_fft[i])
    inverse_c = ifft(c)
    return real(inverse_c)

# inputs sets a, b, c
# return True if there exist n1 in a, n2 in B such that n1+n2 in C
# return False otherwise
# number n which signifies the maximum number in a, b, c
def check_sum_exists(a, b, c, n):
    a_coeffs = [0]*n
    b_coeffs = [0]*n 
    # convert sets a, b into polynomials as provided in the hint
    # a_coeffs and b_coeffs should contain the result
    i = 0
    for item in a:
        a_coeffs[i] = item
        i += 1
    i = 0
    for item in b:
        b_coeffs[i] = item
        i += 1
    # multiply them together
    c_coeffs = polynomial_multiply(a_coeffs, b_coeffs)
    # now this is where i am lost
    # how to determine with c_coeffs?
    return False
    # return True/False

Thanks to all who helped. I figured it out and hopefully this can help anyone who runs into a similar problem. The issue I had was I incorrectly assigned the coefficients for a_coeffs and b_coeffs .

Here is the solution which passed the tests for those interested.

from numpy.fft import fft, ifft
from numpy import real, imag


def check_sum_exists(a, b, c, n):
    a_coeffs = [0] * n
    b_coeffs = [0] * n
    # convert sets a, b into polynomials as provided in the hint
    # a_coeffs and b_coeffs should contain the result
    for coeff in a:
        a_coeffs[coeff] = 1
    for coeff in b:
        b_coeffs[coeff] = 1
    # multiply them together
    c_coeffs = polynomial_multiply(a_coeffs, b_coeffs)
    # use the result to solve the problem at hand
    for coeff in c:
        if c_coeffs[coeff] >= .5:
            return True
    return False
    # return True/False


def polynomial_multiply(a_coeff_list, b_coeff_list):
    # Return the coefficient list of the multiplication
    # of the two polynomials
    # Returned list must be a list of floating point numbers.
    # Please convert list from complex to reals by using the
    # real function in numpy.
    for i in range(len(a_coeff_list) - 1):
        b_coeff_list.append(0)
    for i in range(len(b_coeff_list) - 1):
        a_coeff_list.append(0)
    a_fft = fft(a_coeff_list)
    b_fft = fft(b_coeff_list)
    c = []
    for i in range(len(a_fft)):
        c.append(a_fft[i] * b_fft[i])
    return real(ifft(c))

If we ignore the fact that multiplying very large numbers internally has O(NxM) complexity, we can produce a solution that looks like it's O(n):

def canSum(a,b,c):
    base = len(a)+len(b)+1
    prod = sum(base**n for n in a) * sum(base**n for n in b)
    return any(prod//base**n % base for n in c)


print(canSum([2,3,5],[2,3,4],[3,8]))        # True
print(canSum([10,12,15],[2,7,19],[23,25]))  # False

This works on the basis that, with a large enough common base, multiplying a number formed of the sum of exponents of that base, produces a result that combines every exponent on the left with every exponent on the right of the multiplication:

(b^2 + b^3 + b^5) * (b^2 + b^3 + b^4)

b^4 + b^5 + b^6 + b^5 + b^6 + b^7 + b^7 + b^8 + b^9

b^4 + 2*b^5 + 2*b^6 + 2*b^7 + b^8 + b^9 

A product from which, using integer division, we can extract the factor of any specific power of that base to know if the sum (of exponents) is present with a factor of 1 or more.

To check for a sum of 8:

(b^4 + 2*b^5 + 2*b^6 + 2*b^7 + b^8 + b^9) // b^8  # integer division (floor)

(0 + 0 + 0 + 0 + 1 + b^9) % b --> 1 # non-zero modulo = there is a sum of 8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM