from itertools import product, combinations_with_replacement import time import threading from collections import defaultdict import math # 配置参数(可根据需要修改) TARGET = 270550 # 目标值 BASE_VALUES = [35, 40.5, 58, 67, 73, 90.5] # 基础系数列表 FLUCTUATION = 1.0 # 系数波动范围 MAX_SOLUTIONS = 3 # 每个组合的最大解数量 SOLVER_TIMEOUT = 180 # 求解超时时间(秒) THREE_VAR_THRESHOLD = 220000 # 使用三个变量的阈值 PRODUCT_RANGE_THRESHOLD = 148000 # 乘积范围限制阈值 HIGH_TARGET_THRESHOLD = 260000 # 更高目标值阈值 SHOW_PROGRESS = True # 是否显示进度 MAX_SOLUTIONS_PER_COMB = 100 # 每个组合的最大解数量,用于提前终止 def is_valid_product(p): """检查单个乘积是否在有效范围内""" if TARGET > PRODUCT_RANGE_THRESHOLD: if TARGET > HIGH_TARGET_THRESHOLD: return p <= 115000 # 目标值超过260000时,仅限制最大值 else: return 74000 <= p <= 115000 # 目标值在148000-260000之间时,使用原范围 else: return True # 目标值较小时不限制乘积范围 def find_single_variable_solutions(values): """查找单个数的解(a*x = TARGET)""" solutions = [] for a in values: x = TARGET / a if x.is_integer() and 1 <= x <= 10000 and is_valid_product(a * x): solutions.append((a, x)) if len(solutions) >= MAX_SOLUTIONS: return solutions # 提前终止 return solutions def find_two_variable_solutions(values): """查找两个变量的解(a*x + b*y = TARGET)""" solutions = defaultdict(list) # 生成所有可能的(a, b)组合 for a in values: for b in values: if a == b: # 避免重复 continue # 计算x的有效范围 if max_x < min_x: continue for x in range(min_x, max_x + 1): remainder = TARGET - a * x # 检查remainder是否在可能的范围内 if remainder < 1 or remainder > b * 10000: continue # 检查remainder是否能被b整除 if remainder % b == 0: y = remainder // b if 1 <= y <= 10000 and is_valid_product(b * y): solutions[(a, b)].append((a, x, b, y)) if len(solutions[(a, b)]) >= MAX_SOLUTIONS_PER_COMB: break # 提前终止当前组合的搜索 return solutions def find_three_variable_solutions(values): """优化的三变量求解算法""" solutions = defaultdict(list) # 对系数进行排序,便于剪枝 sorted_values = sorted(values) # 预计算每个系数的有效范围 value_ranges = {} for a in sorted_values: if TARGET > PRODUCT_RANGE_THRESHOLD: if TARGET > HIGH_TARGET_THRESHOLD: else: else: min_x = 1 max_x = 10000 value_ranges[a] = (min_x, max_x) total_combinations = len(sorted_values) * (len(sorted_values) - 1) * (len(sorted_values) - 2) // 6 processed_combinations = 0 # 三重循环,但添加了更多剪枝条件 for i, a in enumerate(sorted_values): min_x, max_x = value_ranges[a] # 计算可能的x值数量,决定步长 x_count = max_x - min_x + 1 x_step = max(1, x_count // 1000) # 自适应步长 for x in range(min_x, max_x + 1, x_step): ax = a * x if not is_valid_product(ax): continue remainder1 = TARGET - ax if TARGET > PRODUCT_RANGE_THRESHOLD: if TARGET > HIGH_TARGET_THRESHOLD: if remainder1 < 0: continue else: if remainder1 < 2 * 74000: continue # 限制b的选择范围,避免重复组合 for j in range(i + 1, len(sorted_values)): b = sorted_values[j] if TARGET > PRODUCT_RANGE_THRESHOLD: if TARGET > HIGH_TARGET_THRESHOLD: else: else: min_y = 1 if max_y < min_y: continue # 计算可能的y值数量,决定步长 y_count = max_y - min_y + 1 y_step = max(1, y_count // 100) # 自适应步长 for y in range(min_y, max_y + 1, y_step): by = b * y if not is_valid_product(by): continue remainder2 = remainder1 - by if TARGET > PRODUCT_RANGE_THRESHOLD: if TARGET > HIGH_TARGET_THRESHOLD: if remainder2 < 0 or remainder2 > 115000: continue else: if remainder2 < 74000 or remainder2 > 115000: continue # 限制c的选择范围 found_solution = False for k in range(j + 1, len(sorted_values)): c = sorted_values[k] # 检查remainder2是否能被c整除 if remainder2 % c != 0: continue z = remainder2 // c if 1 <= z <= 10000 and is_valid_product(c * z): key = (a, b, c) solutions[key].append((a, x, b, y, c, z)) found_solution = True if len(solutions[key]) >= MAX_SOLUTIONS_PER_COMB: break # 提前终止当前组合的搜索 # 如果找到解且达到最大数量,跳出y循环 if found_solution and len(solutions[key]) >= MAX_SOLUTIONS_PER_COMB: break # 跳出y循环 # 进度显示 if SHOW_PROGRESS and j % 10 == 0: print(f"\r三变量组合进度: {processed_combinations}/{total_combinations} 组", end='') processed_combinations += 1 if SHOW_PROGRESS: print(f"\r三变量组合进度: {processed_combinations}/{total_combinations} 组 - 完成") return solutions def find_balanced_solutions(solutions, var_count, num=2): """从所有解中筛选出最平衡的解""" if var_count == 1 or not solutions: return solutions # 单变量或无解时直接返回 # 当目标值超过220000时,不计算平衡解 if TARGET > THREE_VAR_THRESHOLD: return [] # 计算解的平衡性(变量间差异最小) balanced = [] for sol in solutions: vars = sol[1::2] # 提取x, y, z diff = max(vars) - min(vars) balanced.append((diff, sol)) # 按差异排序,取前num个 return [s for _, s in sorted(balanced, key=lambda x: x[0])[:num]] def find_original_solutions(solutions, balanced_solutions, num=3): """从剩余解中获取原始顺序的解""" if not solutions: return [] # 排除已在平衡解中的项 remaining = [s for s in solutions if s not in balanced_solutions] return remaining[:num] def display_solutions(solutions_dict, var_count): """优化的解显示函数""" if not solutions_dict: return print(f"\n找到 {len(solutions_dict)} 组{var_count}变量解:") for i, (coeffs, pair_solutions) in enumerate(sorted(solutions_dict.items()), 1): # 当目标值超过220000时,不显示平衡解 if TARGET > THREE_VAR_THRESHOLD: all_display = pair_solutions[:MAX_SOLUTIONS] print_tag = "[原始解]" else: # 计算平衡解和原始解 balanced = find_balanced_solutions(pair_solutions, var_count) original = find_original_solutions(pair_solutions, balanced) all_display = balanced + original if var_count == 1: a = coeffs print(f"\n{i}. 组合: a={a} ({len(pair_solutions)} 个有效解)") elif var_count == 2: a, b = coeffs print(f"\n{i}. 组合: a={a}, b={b} ({len(pair_solutions)} 个有效解)") else: # var_count == 3 a, b, c = coeffs print(f"\n{i}. 组合: a={a}, b={b}, c={c} ({len(pair_solutions)} 个有效解)") for j, sol in enumerate(all_display, 1): if TARGET > THREE_VAR_THRESHOLD: tag = print_tag else: tag = "[平衡解]" if j <= len(balanced) else "[原始解]" if var_count == 1: a, x = sol print(f" {j}. x={x}, a*x={a*x:.1f}, 总和={a*x:.1f} {tag}") elif var_count == 2: a, x, b, y = sol print(f" {j}. x={x}, y={y}, a*x={a*x:.1f}, b*y={b*y:.1f}, 总和={a*x + b*y:.1f} {tag}") else: # var_count == 3 a, x, b, y, c, z = sol print(f" {j}. x={x}, y={y}, z={z}, " f"a*x={a*x:.1f}, b*y={b*y:.1f}, c*z={c*z:.1f}, " f"总和={a*x + b*y + c*z:.1f} {tag}") def run_with_timeout(func, args=(), kwargs=None, timeout=SOLVER_TIMEOUT): """运行函数并设置超时限制""" if kwargs is None: kwargs = {} result = [] error = [] def wrapper(): try: result.append(func(*args, **kwargs)) except Exception as e: error.append(e) thread = threading.Thread(target=wrapper) thread.daemon = True thread.start() thread.join(timeout) if thread.is_alive(): print(f"警告: {func.__name__} 超时({timeout}秒),跳过此方法") return None if error: return result[0] def main(): print(f"目标值: {TARGET}") # 生成波动后的系数 FLUCTUATED_VALUES = [round(v - FLUCTUATION, 1) for v in BASE_VALUES] # 尝试基础系数 print(f"\n==== 尝试基础系数 ====") # 检查目标值是否超过阈值 if TARGET > THREE_VAR_THRESHOLD: print(f"目标值 {TARGET} 超过阈值 {THREE_VAR_THRESHOLD},只尝试三变量解") base_solutions = { 'single': [], 'two': [], 'three': run_with_timeout(find_three_variable_solutions, args=(BASE_VALUES,)) } # 显示三变量解 if base_solutions['three'] and len(base_solutions['three']) > 0: display_solutions(base_solutions['three'], 3) print(f"\n使用基础系数列表,共找到有效解") return else: print("\n基础系数三变量无解,尝试波动系数") else: # 目标值未超过阈值,按顺序尝试单、双、三变量解 base_solutions = { 'single': run_with_timeout(find_single_variable_solutions, args=(BASE_VALUES,)), 'two': run_with_timeout(find_two_variable_solutions, args=(BASE_VALUES,)), 'three': [] } # 检查是否有解 has_solution = False # 显示单变量解 if base_solutions['single']: has_solution = True display_solutions({a: [sol] for a, sol in zip(BASE_VALUES, base_solutions['single']) if sol}, 1) # 显示双变量解 if base_solutions['two'] and len(base_solutions['two']) > 0: has_solution = True display_solutions(base_solutions['two'], 2) # 单变量和双变量都无解时,尝试三变量解 if not has_solution: print(f"\n==== 单变量和双变量无解,尝试三变量解 ====") base_solutions['three'] = run_with_timeout(find_three_variable_solutions, args=(BASE_VALUES,)) if base_solutions['three'] and len(base_solutions['three']) > 0: has_solution = True display_solutions(base_solutions['three'], 3) # 如果找到解,退出程序 if has_solution: print(f"\n使用基础系数列表,共找到有效解") return # 如果基础系数没有找到解,尝试波动系数 print(f"\n==== 尝试波动系数 ====") # 检查目标值是否超过阈值 if TARGET > THREE_VAR_THRESHOLD: print(f"目标值 {TARGET} 超过阈值 {THREE_VAR_THRESHOLD},只尝试三变量解") fluctuated_solutions = { 'single': [], 'two': [], 'three': run_with_timeout(find_three_variable_solutions, args=(FLUCTUATED_VALUES,)) } # 显示三变量解 if fluctuated_solutions['three'] and len(fluctuated_solutions['three']) > 0: display_solutions(fluctuated_solutions['three'], 3) print(f"\n使用波动系数列表,共找到有效解") return else: # 目标值未超过阈值,按顺序尝试单、双、三变量解 fluctuated_solutions = { 'single': run_with_timeout(find_single_variable_solutions, args=(FLUCTUATED_VALUES,)), 'two': run_with_timeout(find_two_variable_solutions, args=(FLUCTUATED_VALUES,)), 'three': [] } # 重置标志 has_solution = False # 显示单变量解 if fluctuated_solutions['single']: has_solution = True display_solutions({a: [sol] for a, sol in zip(FLUCTUATED_VALUES, fluctuated_solutions['single']) if sol}, 1) # 显示双变量解 if fluctuated_solutions['two'] and len(fluctuated_solutions['two']) > 0: has_solution = True display_solutions(fluctuated_solutions['two'], 2) # 单变量和双变量都无解时,尝试三变量解 if not has_solution: print(f"\n==== 单变量和双变量无解,尝试三变量解 ====") fluctuated_solutions['three'] = run_with_timeout(find_three_variable_solutions, args=(FLUCTUATED_VALUES,)) if fluctuated_solutions['three'] and len(fluctuated_solutions['three']) > 0: has_solution = True display_solutions(fluctuated_solutions['three'], 3) # 如果找到解,退出程序 if has_solution: print(f"\n使用波动系数列表,共找到有效解") return # 如果所有系数集都没有找到解 print("\n没有找到符合条件的解,即使使用波动后的系数列表。") if __name__ == "__main__": main() print(f"\n总耗时: {time.time() - start_time:.2f}秒")
Standard input is empty
目标值: 270550 ==== 尝试基础系数 ==== 目标值 270550 超过阈值 220000,只尝试三变量解 三变量组合进度: 22108/20 组 - 完成 找到 17 组3变量解: 1. 组合: a=35, b=40.5, c=58 (67 个有效解) 1. x=1303, y=2806, z=1919.0, a*x=45605.0, b*y=113643.0, c*z=111302.0, 总和=270550.0 [原始解] 2. x=1489, y=2598, z=1952.0, a*x=52115.0, b*y=105219.0, c*z=113216.0, 总和=270550.0 [原始解] 3. x=1537, y=2810, z=1775.0, a*x=53795.0, b*y=113805.0, c*z=102950.0, 总和=270550.0 [原始解] 2. 组合: a=35, b=40.5, c=67 (44 个有效解) 1. x=1540, y=2810, z=1535.0, a*x=53900.0, b*y=113805.0, c*z=102845.0, 总和=270550.0 [原始解] 2. x=1588, y=2704, z=1574.0, a*x=55580.0, b*y=109512.0, c*z=105458.0, 总和=270550.0 [原始解] 3. x=1729, y=2602, z=1562.0, a*x=60515.0, b*y=105381.0, c*z=104654.0, 总和=270550.0 [原始解] 3. 组合: a=35, b=40.5, c=73 (54 个有效解) 1. x=1294, y=2806, z=1529.0, a*x=45290.0, b*y=113643.0, c*z=111617.0, 总和=270550.0 [原始解] 2. x=1576, y=2492, z=1568.0, a*x=55160.0, b*y=100926.0, c*z=114464.0, 总和=270550.0 [原始解] 3. x=1591, y=2598, z=1502.0, a*x=55685.0, b*y=105219.0, c*z=109646.0, 总和=270550.0 [原始解] 4. 组合: a=35, b=40.5, c=90.5 (89 个有效解) 1. x=1342, y=2696, z=1264.0, a*x=46970.0, b*y=109188.0, c*z=114392.0, 总和=270550.0 [原始解] 2. x=1375, y=2701, z=1249.0, a*x=48125.0, b*y=109390.5, c*z=113034.5, 总和=270550.0 [原始解] 3. x=1453, y=2647, z=1243.0, a*x=50855.0, b*y=107203.5, c*z=112491.5, 总和=270550.0 [原始解] 5. 组合: a=35, b=58, c=67 (186 个有效解) 1. x=1381, y=1901, z=1671, a*x=48335.0, b*y=110258.0, c*z=111957.0, 总和=270550.0 [原始解] 2. x=1408, y=1939, z=1624, a*x=49280.0, b*y=112462.0, c*z=108808.0, 总和=270550.0 [原始解] 3. x=1462, y=1814, z=1704, a*x=51170.0, b*y=105212.0, c*z=114168.0, 总和=270550.0 [原始解] 6. 组合: a=35, b=58, c=73 (164 个有效解) 1. x=1474, y=1925, z=1470, a*x=51590.0, b*y=111650.0, c*z=107310.0, 总和=270550.0 [原始解] 2. x=1489, y=1814, z=1551, a*x=52115.0, b*y=105212.0, c*z=113223.0, 总和=270550.0 [原始解] 3. x=1552, y=1888, z=1462, a*x=54320.0, b*y=109504.0, c*z=106726.0, 总和=270550.0 [原始解] 7. 组合: a=35, b=58, c=90.5 (59 个有效解) 1. x=1528, y=1814, z=1236.0, a*x=53480.0, b*y=105212.0, c*z=111858.0, 总和=270550.0 [原始解] 2. x=1684, y=1801, z=1184.0, a*x=58940.0, b*y=104458.0, c*z=107152.0, 总和=270550.0 [原始解] 3. x=1696, y=1981, z=1064.0, a*x=59360.0, b*y=114898.0, c*z=96292.0, 总和=270550.0 [原始解] 8. 组合: a=35, b=67, c=73 (212 个有效解) 1. x=1345, y=1651, z=1546, a*x=47075.0, b*y=110617.0, c*z=112858.0, 总和=270550.0 [原始解] 2. x=1417, y=1633, z=1528, a*x=49595.0, b*y=109411.0, c*z=111544.0, 总和=270550.0 [原始解] 3. x=1435, y=1665, z=1490, a*x=50225.0, b*y=111555.0, c*z=108770.0, 总和=270550.0 [原始解] 9. 组合: a=35, b=67, c=90.5 (65 个有效解) 1. x=1354, y=1618, z=1268.0, a*x=47390.0, b*y=108406.0, c*z=114754.0, 总和=270550.0 [原始解] 2. x=1414, y=1665, z=1210.0, a*x=49490.0, b*y=111555.0, c*z=109505.0, 总和=270550.0 [原始解] 3. x=1558, y=1633, z=1178.0, a*x=54530.0, b*y=109411.0, c*z=106609.0, 总和=270550.0 [原始解] 10. 组合: a=35, b=73, c=90.5 (88 个有效解) 1. x=1516, y=1509, z=1186.0, a*x=53060.0, b*y=110157.0, c*z=107333.0, 总和=270550.0 [原始解] 2. x=1621, y=1538, z=1122.0, a*x=56735.0, b*y=112274.0, c*z=101541.0, 总和=270550.0 [原始解] 3. x=1639, y=1393, z=1232.0, a*x=57365.0, b*y=101689.0, c*z=111496.0, 总和=270550.0 [原始解] 11. 组合: a=40.5, b=58, c=90.5 (102 个有效解) 1. x=1175, y=1939, z=1221.0, a*x=47587.5, b*y=112462.0, c*z=110500.5, 总和=270550.0 [原始解] 2. x=1231, y=1825, z=1269.0, a*x=49855.5, b*y=105850.0, c*z=114844.5, 总和=270550.0 [原始解] 3. x=1347, y=1925, z=1153.0, a*x=54553.5, b*y=111650.0, c*z=104346.5, 总和=270550.0 [原始解] 12. 组合: a=40.5, b=67, c=90.5 (112 个有效解) 1. x=1283, y=1697, z=1159.0, a*x=51961.5, b*y=113699.0, c*z=104889.5, 总和=270550.0 [原始解] 2. x=1327, y=1665, z=1163.0, a*x=53743.5, b*y=111555.0, c*z=105251.5, 总和=270550.0 [原始解] 3. x=1371, y=1633, z=1167.0, a*x=55525.5, b*y=109411.0, c*z=105613.5, 总和=270550.0 [原始解] 13. 组合: a=40.5, b=73, c=90.5 (105 个有效解) 1. x=1165, y=1561, z=1209.0, a*x=47182.5, b*y=113953.0, c*z=109414.5, 总和=270550.0 [原始解] 2. x=1311, y=1480, z=1209.0, a*x=53095.5, b*y=108040.0, c*z=109414.5, 总和=270550.0 [原始解] 3. x=1345, y=1538, z=1147.0, a*x=54472.5, b*y=112274.0, c*z=103803.5, 总和=270550.0 [原始解] 14. 组合: a=58, b=67, c=73 (359 个有效解) 1. x=735, y=1701, z=1561, a*x=42630.0, b*y=113967.0, c*z=113953.0, 总和=270550.0 [原始解] 2. x=771, y=1684, z=1548, a*x=44718.0, b*y=112828.0, c*z=113004.0, 总和=270550.0 [原始解] 3. x=828, y=1651, z=1533, a*x=48024.0, b*y=110617.0, c*z=111909.0, 总和=270550.0 [原始解] 15. 组合: a=58, b=67, c=90.5 (127 个有效解) 1. x=797, y=1684, z=1232.0, a*x=46226.0, b*y=112828.0, c*z=111496.0, 总和=270550.0 [原始解] 2. x=832, y=1651, z=1234.0, a*x=48256.0, b*y=110617.0, c*z=111677.0, 总和=270550.0 [原始解] 3. x=860, y=1697, z=1182.0, a*x=49880.0, b*y=113699.0, c*z=106971.0, 总和=270550.0 [原始解] 16. 组合: a=58, b=73, c=90.5 (140 个有效解) 1. x=806, y=1531, z=1238.0, a*x=46748.0, b*y=111763.0, c*z=112039.0, 总和=270550.0 [原始解] 2. x=850, y=1501, z=1234.0, a*x=49300.0, b*y=109573.0, c*z=111677.0, 总和=270550.0 [原始解] 3. x=898, y=1567, z=1150.0, a*x=52084.0, b*y=114391.0, c*z=104075.0, 总和=270550.0 [原始解] 17. 组合: a=67, b=73, c=90.5 (131 个有效解) 1. x=698, y=1501, z=1262.0, a*x=46766.0, b*y=109573.0, c*z=114211.0, 总和=270550.0 [原始解] 2. x=755, y=1471, z=1244.0, a*x=50585.0, b*y=107383.0, c*z=112582.0, 总和=270550.0 [原始解] 3. x=765, y=1561, z=1164.0, a*x=51255.0, b*y=113953.0, c*z=105342.0, 总和=270550.0 [原始解] 使用基础系数列表,共找到有效解 总耗时: 1.14秒