from itertools import product, combinations_with_replacement import time import threading from collections import defaultdict import math # 配置参数(可根据需要修改) TARGET = 148500 # 目标值 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 # 乘积范围限制阈值 SHOW_PROGRESS = True # 是否显示进度 def is_valid_product(p): """检查单个乘积是否在有效范围内""" if TARGET > PRODUCT_RANGE_THRESHOLD: return 74000 <= p <= 115000 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)) 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)) 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: 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] for x in range(min_x, max_x + 1): ax = a * x if not is_valid_product(ax): continue remainder1 = TARGET - ax if TARGET > PRODUCT_RANGE_THRESHOLD and remainder1 < 2 * 74000: continue # 限制b的选择范围,避免重复组合 for j in range(i + 1, len(sorted_values)): b = sorted_values[j] if TARGET > PRODUCT_RANGE_THRESHOLD: else: min_y = 1 if max_y < min_y: continue for y in range(min_y, max_y + 1): by = b * y if not is_valid_product(by): continue remainder2 = remainder1 - by if TARGET > PRODUCT_RANGE_THRESHOLD and (remainder2 < 74000 or remainder2 > 115000): continue # 限制c的选择范围 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)) # 进度显示 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 # 单变量或无解时直接返回 # 当目标值超过阈值时,不计算平衡解 if TARGET > PRODUCT_RANGE_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): # 当目标值超过阈值时,不显示平衡解 if TARGET > PRODUCT_RANGE_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 > PRODUCT_RANGE_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
目标值: 148500 ==== 尝试基础系数 ==== 找到 30 组2变量解: 1. 组合: a=35, b=40.5 (14 个有效解) 1. x=1026, y=2780.0, a*x=35910.0, b*y=112590.0, 总和=148500.0 [原始解] 2. x=1107, y=2710.0, a*x=38745.0, b*y=109755.0, 总和=148500.0 [原始解] 3. x=1188, y=2640.0, a*x=41580.0, b*y=106920.0, 总和=148500.0 [原始解] 2. 组合: a=35, b=58 (20 个有效解) 1. x=970, y=1975, a*x=33950.0, b*y=114550.0, 总和=148500.0 [原始解] 2. x=1028, y=1940, a*x=35980.0, b*y=112520.0, 总和=148500.0 [原始解] 3. x=1086, y=1905, a*x=38010.0, b*y=110490.0, 总和=148500.0 [原始解] 3. 组合: a=35, b=67 (18 个有效解) 1. x=979, y=1705, a*x=34265.0, b*y=114235.0, 总和=148500.0 [原始解] 2. x=1046, y=1670, a*x=36610.0, b*y=111890.0, 总和=148500.0 [原始解] 3. x=1113, y=1635, a*x=38955.0, b*y=109545.0, 总和=148500.0 [原始解] 4. 组合: a=35, b=73 (16 个有效解) 1. x=1010, y=1550, a*x=35350.0, b*y=113150.0, 总和=148500.0 [原始解] 2. x=1083, y=1515, a*x=37905.0, b*y=110595.0, 总和=148500.0 [原始解] 3. x=1156, y=1480, a*x=40460.0, b*y=108040.0, 总和=148500.0 [原始解] 5. 组合: a=35, b=90.5 (7 个有效解) 1. x=959, y=1270.0, a*x=33565.0, b*y=114935.0, 总和=148500.0 [原始解] 2. x=1140, y=1200.0, a*x=39900.0, b*y=108600.0, 总和=148500.0 [原始解] 3. x=1321, y=1130.0, a*x=46235.0, b*y=102265.0, 总和=148500.0 [原始解] 6. 组合: a=40.5, b=35 (14 个有效解) 1. x=890, y=3213.0, a*x=36045.0, b*y=112455.0, 总和=148500.0 [原始解] 2. x=960, y=3132.0, a*x=38880.0, b*y=109620.0, 总和=148500.0 [原始解] 3. x=1030, y=3051.0, a*x=41715.0, b*y=106785.0, 总和=148500.0 [原始解] 7. 组合: a=40.5, b=58 (9 个有效解) 1. x=844, y=1971.0, a*x=34182.0, b*y=114318.0, 总和=148500.0 [原始解] 2. x=960, y=1890.0, a*x=38880.0, b*y=109620.0, 总和=148500.0 [原始解] 3. x=1076, y=1809.0, a*x=43578.0, b*y=104922.0, 总和=148500.0 [原始解] 8. 组合: a=40.5, b=67 (7 个有效解) 1. x=942, y=1647.0, a*x=38151.0, b*y=110349.0, 总和=148500.0 [原始解] 2. x=1076, y=1566.0, a*x=43578.0, b*y=104922.0, 总和=148500.0 [原始解] 3. x=1210, y=1485.0, a*x=49005.0, b*y=99495.0, 总和=148500.0 [原始解] 9. 组合: a=40.5, b=73 (7 个有效解) 1. x=844, y=1566.0, a*x=34182.0, b*y=114318.0, 总和=148500.0 [原始解] 2. x=990, y=1485.0, a*x=40095.0, b*y=108405.0, 总和=148500.0 [原始解] 3. x=1136, y=1404.0, a*x=46008.0, b*y=102492.0, 总和=148500.0 [原始解] 10. 组合: a=40.5, b=90.5 (6 个有效解) 1. x=831, y=1269.0, a*x=33655.5, b*y=114844.5, 总和=148500.0 [原始解] 2. x=1012, y=1188.0, a*x=40986.0, b*y=107514.0, 总和=148500.0 [原始解] 3. x=1193, y=1107.0, a*x=48316.5, b*y=100183.5, 总和=148500.0 [原始解] 11. 组合: a=58, b=35 (20 个有效解) 1. x=610, y=3232, a*x=35380.0, b*y=113120.0, 总和=148500.0 [原始解] 2. x=645, y=3174, a*x=37410.0, b*y=111090.0, 总和=148500.0 [原始解] 3. x=680, y=3116, a*x=39440.0, b*y=109060.0, 总和=148500.0 [原始解] 12. 组合: a=58, b=40.5 (9 个有效解) 1. x=594, y=2816.0, a*x=34452.0, b*y=114048.0, 总和=148500.0 [原始解] 2. x=675, y=2700.0, a*x=39150.0, b*y=109350.0, 总和=148500.0 [原始解] 3. x=756, y=2584.0, a*x=43848.0, b*y=104652.0, 总和=148500.0 [原始解] 13. 组合: a=58, b=67 (11 个有效解) 1. x=585, y=1710, a*x=33930.0, b*y=114570.0, 总和=148500.0 [原始解] 2. x=652, y=1652, a*x=37816.0, b*y=110684.0, 总和=148500.0 [原始解] 3. x=719, y=1594, a*x=41702.0, b*y=106798.0, 总和=148500.0 [原始解] 14. 组合: a=58, b=73 (10 个有效解) 1. x=612, y=1548, a*x=35496.0, b*y=113004.0, 总和=148500.0 [原始解] 2. x=685, y=1490, a*x=39730.0, b*y=108770.0, 总和=148500.0 [原始解] 3. x=758, y=1432, a*x=43964.0, b*y=104536.0, 总和=148500.0 [原始解] 15. 组合: a=58, b=90.5 (4 个有效解) 1. x=638, y=1232.0, a*x=37004.0, b*y=111496.0, 总和=148500.0 [原始解] 2. x=819, y=1116.0, a*x=47502.0, b*y=100998.0, 总和=148500.0 [原始解] 3. x=1000, y=1000.0, a*x=58000.0, b*y=90500.0, 总和=148500.0 [原始解] 16. 组合: a=67, b=35 (18 个有效解) 1. x=515, y=3257, a*x=34505.0, b*y=113995.0, 总和=148500.0 [原始解] 2. x=550, y=3190, a*x=36850.0, b*y=111650.0, 总和=148500.0 [原始解] 3. x=585, y=3123, a*x=39195.0, b*y=109305.0, 总和=148500.0 [原始解] 17. 组合: a=67, b=40.5 (8 个有效解) 1. x=513, y=2818.0, a*x=34371.0, b*y=114129.0, 总和=148500.0 [原始解] 2. x=594, y=2684.0, a*x=39798.0, b*y=108702.0, 总和=148500.0 [原始解] 3. x=675, y=2550.0, a*x=45225.0, b*y=103275.0, 总和=148500.0 [原始解] 18. 组合: a=67, b=58 (10 个有效解) 1. x=550, y=1925, a*x=36850.0, b*y=111650.0, 总和=148500.0 [原始解] 2. x=608, y=1858, a*x=40736.0, b*y=107764.0, 总和=148500.0 [原始解] 3. x=666, y=1791, a*x=44622.0, b*y=103878.0, 总和=148500.0 [原始解] 19. 组合: a=67, b=73 (9 个有效解) 1. x=508, y=1568, a*x=34036.0, b*y=114464.0, 总和=148500.0 [原始解] 2. x=581, y=1501, a*x=38927.0, b*y=109573.0, 总和=148500.0 [原始解] 3. x=654, y=1434, a*x=43818.0, b*y=104682.0, 总和=148500.0 [原始解] 20. 组合: a=67, b=90.5 (4 个有效解) 1. x=555, y=1230.0, a*x=37185.0, b*y=111315.0, 总和=148500.0 [原始解] 2. x=736, y=1096.0, a*x=49312.0, b*y=99188.0, 总和=148500.0 [原始解] 3. x=917, y=962.0, a*x=61439.0, b*y=87061.0, 总和=148500.0 [原始解] 21. 组合: a=73, b=35 (16 个有效解) 1. x=465, y=3273, a*x=33945.0, b*y=114555.0, 总和=148500.0 [原始解] 2. x=500, y=3200, a*x=36500.0, b*y=112000.0, 总和=148500.0 [原始解] 3. x=535, y=3127, a*x=39055.0, b*y=109445.0, 总和=148500.0 [原始解] 22. 组合: a=73, b=40.5 (7 个有效解) 1. x=513, y=2742.0, a*x=37449.0, b*y=111051.0, 总和=148500.0 [原始解] 2. x=594, y=2596.0, a*x=43362.0, b*y=105138.0, 总和=148500.0 [原始解] 3. x=675, y=2450.0, a*x=49275.0, b*y=99225.0, 总和=148500.0 [原始解] 23. 组合: a=73, b=58 (9 个有效解) 1. x=504, y=1926, a*x=36792.0, b*y=111708.0, 总和=148500.0 [原始解] 2. x=562, y=1853, a*x=41026.0, b*y=107474.0, 总和=148500.0 [原始解] 3. x=620, y=1780, a*x=45260.0, b*y=103240.0, 总和=148500.0 [原始解] 24. 组合: a=73, b=67 (8 个有效解) 1. x=496, y=1676, a*x=36208.0, b*y=112292.0, 总和=148500.0 [原始解] 2. x=563, y=1603, a*x=41099.0, b*y=107401.0, 总和=148500.0 [原始解] 3. x=630, y=1530, a*x=45990.0, b*y=102510.0, 总和=148500.0 [原始解] 25. 组合: a=73, b=90.5 (3 个有效解) 1. x=616, y=1144.0, a*x=44968.0, b*y=103532.0, 总和=148500.0 [原始解] 2. x=797, y=998.0, a*x=58181.0, b*y=90319.0, 总和=148500.0 [原始解] 3. x=978, y=852.0, a*x=71394.0, b*y=77106.0, 总和=148500.0 [原始解] 26. 组合: a=90.5, b=35 (6 个有效解) 1. x=430, y=3131.0, a*x=38915.0, b*y=109585.0, 总和=148500.0 [原始解] 2. x=500, y=2950.0, a*x=45250.0, b*y=103250.0, 总和=148500.0 [原始解] 3. x=570, y=2769.0, a*x=51585.0, b*y=96915.0, 总和=148500.0 [原始解] 27. 组合: a=90.5, b=40.5 (6 个有效解) 1. x=378, y=2822.0, a*x=34209.0, b*y=114291.0, 总和=148500.0 [原始解] 2. x=459, y=2641.0, a*x=41539.5, b*y=106960.5, 总和=148500.0 [原始解] 3. x=540, y=2460.0, a*x=48870.0, b*y=99630.0, 总和=148500.0 [原始解] 28. 组合: a=90.5, b=58 (4 个有效解) 1. x=420, y=1905.0, a*x=38010.0, b*y=110490.0, 总和=148500.0 [原始解] 2. x=536, y=1724.0, a*x=48508.0, b*y=99992.0, 总和=148500.0 [原始解] 3. x=652, y=1543.0, a*x=59006.0, b*y=89494.0, 总和=148500.0 [原始解] 29. 组合: a=90.5, b=67 (3 个有效解) 1. x=426, y=1641.0, a*x=38553.0, b*y=109947.0, 总和=148500.0 [原始解] 2. x=560, y=1460.0, a*x=50680.0, b*y=97820.0, 总和=148500.0 [原始解] 3. x=694, y=1279.0, a*x=62807.0, b*y=85693.0, 总和=148500.0 [原始解] 30. 组合: a=90.5, b=73 (3 个有效解) 1. x=414, y=1521.0, a*x=37467.0, b*y=111033.0, 总和=148500.0 [原始解] 2. x=560, y=1340.0, a*x=50680.0, b*y=97820.0, 总和=148500.0 [原始解] 3. x=706, y=1159.0, a*x=63893.0, b*y=84607.0, 总和=148500.0 [原始解] 使用基础系数列表,共找到有效解 总耗时: 0.04秒