【问题标题】:Determining if there exists numbers n1, n2 in a, b and n3 in c such that n1 + n2 = n3 [ftt, polynomial multiplication]确定在 a、b 和 c 中是否存在数字 n1、n2 使得 n1 + n2 = n3 [ftt,多项式乘法]
【发布时间】:2021-10-16 20:45:05
【问题描述】:

您好,我正在处理一个似乎超出我能力范围的问题,因此非常感谢任何提示、阅读材料的指示等。这就是问题所在:

给定 3 个数字子集 a, b, c ⊆ {0, ..., n}。在 nlog(n) 中,检查 a 中是否存在数字 n1、n2、c 中的 b 和 n3,其中 n1 + n2 = n3。

我被提示将 a 和 b 转换为多项式系数,并使用 ftt 使用多项式乘法来将 a 和 b 的系数相乘。

现在我卡住的地方是在得到多项式乘法的结果后,接下来我该怎么办?

先谢谢你了。

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

【问题讨论】:

  • @don'ttalkjustcode 正确我只是使用了问题中的措辞。我将编辑以简化。谢谢。
  • 我想你一定误解了问题和给你的提示。正如您在问题中所说,这正是3SUM problem,并且没有已知的算法可以在 O(n log n) 时间内解决它。但是,有一种算法可以在 O(n + N log N) 时间内解决它,其中 N 是三个集合中任何数字的最大绝对值,并且这些数字都是整数。我怀疑您错过了有关问题和提示的详细信息。维基百科为此引用了著名的Introduction to Algorithms (CLRS) 教科书。
  • @kaya3 啊...我想这可能就是他们说“子集”的原因。超集为 {0, 1, 2, ..., n}。
  • 用非零系数读出a(x) b(x)中x的幂,检验是否有属于c的。
  • c = set(c); return any(j in c for (j, k) in enumerate(c_coeffs) if round(k)) 或类似的东西。

标签: python algorithm fft polynomial-math


【解决方案1】:

感谢所有帮助过的人。我想通了,希望这可以帮助遇到类似问题的任何人。我遇到的问题是我错误地为a_coeffsb_coeffs 分配了系数。

这是通过测试的解决方案。

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))

【讨论】:

  • 阈值设置为 1 有点危险,因为您使用的是浮点 FFT。我建议将 0.5 作为替代阈值。 (另一种处理方法是使用数论变换而不是 DFT。)
  • @DavidEisenstat 我根据你的建议更新到 0.5 谢谢!
猜你喜欢
  • 2014-05-07
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多