fork download
  1. from itertools import combinations
  2. import time
  3. import threading
  4. from collections import defaultdict
  5. import math
  6. from multiprocessing import Pool, freeze_support # 增加freeze_support导入
  7.  
  8. # 配置参数(针对目标值268668优化)
  9. TARGET = 248790 # 目标值
  10. BASE_VALUES = [38.5,44,61,70.5,75.5,93]# 基础系数列表
  11. FLUCTUATION = 1.0 # 系数波动范围
  12. MAX_SOLUTIONS = 5 # 每个组合的最大解数量
  13. SOLVER_TIMEOUT = 180 # 适当减少超时时间
  14. THREE_VAR_THRESHOLD = 259000 # 使用三个变量的阈值
  15. PRODUCT_RANGE_THRESHOLD = 129000 # 乘积范围限制阈值
  16. HIGH_TARGET_THRESHOLD = 259000 # 更高目标值阈值
  17. SHOW_PROGRESS = True # 是否显示进度
  18. MAX_SOLUTIONS_PER_COMB = 100 # 适当减少最大解数量
  19. USE_MULTIPROCESSING = False # 暂时禁用多进程以排查错误
  20.  
  21. def is_valid_product(p):
  22. """检查单个乘积是否在有效范围内"""
  23. try:
  24. if TARGET > HIGH_TARGET_THRESHOLD: # TARGET > 259000
  25. return p <= 129000 # 单个乘积上限
  26. elif TARGET > PRODUCT_RANGE_THRESHOLD: # 129000 < TARGET <= 259000
  27. return 74000 <= p <= 129000 # 单个乘积范围
  28. else: # TARGET <= 129000
  29. return True # 小目标值取消所有限制
  30. except:
  31. return False
  32.  
  33. def find_single_variable_solutions(values):
  34. """查找单个数的解(a*x = TARGET)"""
  35. solutions = []
  36. for a in values:
  37. try: # 增加异常处理
  38. if a == 0:
  39. continue
  40. quotient = TARGET / a
  41. if not math.isclose(quotient, round(quotient), abs_tol=1e-9):
  42. continue
  43. x = round(quotient)
  44. if 1 <= x <= 10000 and is_valid_product(a * x):
  45. solutions.append((a, x))
  46. if len(solutions) >= MAX_SOLUTIONS:
  47. break
  48. except Exception as e:
  49. print(f"处理单变量 {a} 时出错: {e}")
  50. return solutions
  51.  
  52. # 其他函数保持不变,但为每个主要函数添加基本的异常处理
  53.  
  54. def main():
  55. try: # 主函数增加异常捕获
  56. print(f"目标值: {TARGET}")
  57. print(f"单个乘积上限: 129000")
  58. print(f"理论上至少需要 {math.ceil(TARGET / 129000)} 个乘积项")
  59.  
  60. # 生成波动后的系数
  61. FLUCTUATED_VALUES = [round(v - FLUCTUATION, 1) for v in BASE_VALUES] + \
  62. [round(v + FLUCTUATION, 1) for v in BASE_VALUES]
  63. FLUCTUATED_VALUES = list(set(FLUCTUATED_VALUES)) # 去重
  64.  
  65. # 尝试基础系数
  66. print(f"\n==== 尝试基础系数 ====")
  67.  
  68. base_solutions = {
  69. 'single': run_with_timeout(find_single_variable_solutions, args=(BASE_VALUES,)),
  70. 'two': run_with_timeout(find_two_variable_solutions, args=(BASE_VALUES,)),
  71. 'three': []
  72. }
  73.  
  74. has_solution = False
  75.  
  76. # 显示单变量解
  77. if base_solutions['single']:
  78. has_solution = True
  79. display_solutions({a: [sol] for a, sol in zip(BASE_VALUES, base_solutions['single']) if sol}, 1)
  80.  
  81. # 显示双变量解
  82. if base_solutions['two'] and len(base_solutions['two']) > 0:
  83. has_solution = True
  84. display_solutions(base_solutions['two'], 2)
  85.  
  86. # 尝试三变量解
  87. print(f"\n==== 尝试三变量解 ====")
  88. base_solutions['three'] = run_with_timeout(find_three_variable_solutions, args=(BASE_VALUES,))
  89.  
  90. if base_solutions['three'] and len(base_solutions['three']) > 0:
  91. has_solution = True
  92. display_solutions(base_solutions['three'], 3)
  93.  
  94. if has_solution:
  95. print(f"\n使用基础系数列表,共找到有效解")
  96. return
  97.  
  98. # 如果基础系数没有找到解,尝试波动系数
  99. print(f"\n==== 尝试波动系数 ====")
  100.  
  101. fluctuated_solutions = {
  102. 'single': run_with_timeout(find_single_variable_solutions, args=(FLUCTUATED_VALUES,)),
  103. 'two': run_with_timeout(find_two_variable_solutions, args=(FLUCTUATED_VALUES,)),
  104. 'three': []
  105. }
  106.  
  107. has_solution = False
  108.  
  109. # 显示单变量解
  110. if fluctuated_solutions['single']:
  111. has_solution = True
  112. display_solutions({a: [sol] for a, sol in zip(FLUCTUATED_VALUES, fluctuated_solutions['single']) if sol}, 1)
  113.  
  114. # 显示双变量解
  115. if fluctuated_solutions['two'] and len(fluctuated_solutions['two']) > 0:
  116. has_solution = True
  117. display_solutions(fluctuated_solutions['two'], 2)
  118.  
  119. # 尝试三变量解
  120. print(f"\n==== 尝试三变量解 ====")
  121. fluctuated_solutions['three'] = run_with_timeout(find_three_variable_solutions, args=(FLUCTUATED_VALUES,))
  122.  
  123. if fluctuated_solutions['three'] and len(fluctuated_solutions['three']) > 0:
  124. has_solution = True
  125. display_solutions(fluctuated_solutions['three'], 3)
  126.  
  127. if has_solution:
  128. print(f"\n使用波动系数列表,共找到有效解")
  129. return
  130.  
  131. # 如果所有系数集都没有找到解
  132. print("\n没有找到符合条件的解,建议:")
  133. print("1. 放宽单个乘积上限(当前为129000)")
  134. print("2. 增加系数列表中的数值")
  135. print("3. 延长求解超时时间")
  136. except Exception as e:
  137. print(f"程序执行出错: {e}")
  138.  
  139. if __name__ == "__main__":
  140. freeze_support() # 增加多进程支持
  141. start_time = time.time()
  142. main()
  143. print(f"\n总耗时: {time.time() - start_time:.2f}秒")
Success #stdin #stdout 0.06s 11184KB
stdin
Standard input is empty
stdout
目标值: 248790
单个乘积上限: 129000
理论上至少需要 2 个乘积项

==== 尝试基础系数 ====
程序执行出错: name 'run_with_timeout' is not defined

总耗时: 0.00秒