algorithm - 多项式时间算法使右手总和与左手总和的组合相等?

标签 algorithm np

我必须为这个问题编写多项式时间算法,如果左边的数字与右边的数字不匹配,应该说接受或拒绝。 您也可以对数字进行分组,示例如下

X & Y .......... & N = SUM where X, Y, and N can be any integer

Case 1: 4 & 6 & 10 = 14 , accepts 
So case 1 accepts because first number and the third number together sum up to 14.  
Case 2: 4 & 6 & 10 = 8 rejects
Case 3: 4 & 6 & 10 = 6 , accepts 
Case 4: 4 & 6 & 10 = 11 rejects

Some more test cases:
Case 1: 4 & 6 & 10 = 4 , accepts 
Case 2: 4 & 6 & 10 = 21 rejects
Case 3: 4 & 6 & 10 = 20 , accepts 
Case 4: 4 & 6 & 10 = 17 rejects
Case 5: 4 & 6 & 10 = 16 , accepts 
Case 6: 4 & 6 & 10 = 200 rejects

Case 7: 4 & 6 & 10 = 111 , rejects 
Case 8: 4 & 6 & 10 = 7 rejects

some more test cases 

Case 1 : 1 & 1 & 1 = 3 accepts
Case 2 : 1 & 1 & 1 & 2 & 2 & 22 = 29 accepts

为此,我该如何编写多项式时间算法? 是NP问题吗?

最佳答案

在我看来,这也是子集求和问题,它是 NP-complete(这意味着多项式时间解不存在,除非 P=NP) 但 can be solved in pseudo-polynomial time (即数字数量和总和的的时间多项式)使用动态规划。

关于algorithm - 多项式时间算法使右手总和与左手总和的组合相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43684848/

相关文章:

c - 我的算法有什么问题?

algorithm - 递推关系的中间步 T(n) = 2T(n/2)+ n/log(n)

c++ - 计算许多数字的几何平均值的有效方法

c# - 从包含 m 个项目的集合 S 中选择到长度为 N (N>m) 的另一个列表中的排列

algorithm - 给定一个黑盒子找到一个集合的相等划分,如果存在则返回 true

创建学校时间表的算法

algorithm - 为什么考虑 NP 而不是 P?

c - 11线程程序段错误

algorithm - NP完全性和可还原性

algorithm - 无向图中给定两个顶点之间的最长简单路径