Python 我在我的代码中实现了这一点?你只是想修复你的代码还是找到最好的解决方案?如果有很多跳转大小,这很有效。但这可能是过火了。对于两个跳跃大小,问题可以表述为d1*y1+d2*y2=x-k,并使用基于gcd的求解线性丢番图方程的简单算法进行求解。是的,我认为我犯

Python 我在我的代码中实现了这一点?你只是想修复你的代码还是找到最好的解决方案?如果有很多跳转大小,这很有效。但这可能是过火了。对于两个跳跃大小,问题可以表述为d1*y1+d2*y2=x-k,并使用基于gcd的求解线性丢番图方程的简单算法进行求解。是的,我认为我犯,python,python-3.x,list,Python,Python 3.x,List,我在我的代码中实现了这一点?你只是想修复你的代码还是找到最好的解决方案?如果有很多跳转大小,这很有效。但这可能是过火了。对于两个跳跃大小,问题可以表述为d1*y1+d2*y2=x-k,并使用基于gcd的求解线性丢番图方程的简单算法进行求解。是的,我认为我犯了一个常见错误,即用“明显的”解进行跳跃,而不是首先更深入地思考问题(或者读到别人对它的看法的评论),然后我发现,即使是对我的方法来说,这也是一个难题。 from itertools import product def position(k


我在我的代码中实现了这一点?你只是想修复你的代码还是找到最好的解决方案?如果有很多跳转大小,这很有效。但这可能是过火了。对于两个跳跃大小,问题可以表述为
d1*y1+d2*y2=x-k
,并使用基于gcd的求解线性丢番图方程的简单算法进行求解。是的,我认为我犯了一个常见错误,即用“明显的”解进行跳跃,而不是首先更深入地思考问题(或者读到别人对它的看法的评论),然后我发现,即使是对我的方法来说,这也是一个难题。
from itertools import product

def position(k, d1, d2, p, count):
    count += 1
    arr = [[k], ['+', '-'], [d1, d2]]
    x = list(product(*arr))
    for i in range(len(x)):
        if x[i][1] == '+':
            x[i] = x[i][0] + x[i][2]
        else:
            x[i] = x[i][0] - x[i][2]

        if x[i] == p:
            return count

    for i in range(2):
        position(x[i], d1, d2, p, count)

if __name__ == "__main__":
    y = [int(i) for i in input().split()]
    k = y[0]
    d1 = y[1]
    d2 = y[2]
    p = y[3]
    print(position(k, d1, d2, p, 0))
x = [(10,'+',4), (10,'+',6), (10,'-',4), (10,'-',6)]
x = [(14,'+',4), (14,'+',6), (14,'-',4), (14,'-',6)]
from pulp import *

def position(k, d1, d2, p):
    prob = LpProblem("Minimum Steps", LpMinimize)

    # define decision variables (values to be chosen by solver)
    up1 = LpVariable('up1', lowBound=0, cat='Integer')
    down1 = LpVariable('down1', lowBound=0, cat='Integer')
    up2 = LpVariable('up2', lowBound=0, cat='Integer')
    down2 = LpVariable('down2', lowBound=0, cat='Integer')

    # define objective function (expression to minimize)
    num_steps = up1 + down1 + up2 + down2
    prob += num_steps

    # define main constraint (rule to enforce)
    prob += (k + up1*d1 - down1*d1 + up2*d2 - down2*d2 == p)

    # solve with a time limit, because otherwise CBC may search forever
    prob.solve(PULP_CBC_CMD(maxSeconds=2))

    # if you have CPLEX installed, it can detect infeasibility immediately
    # prob.solve(CPLEX_CMD())

    status = LpStatus[prob.status]
    solution = [str(k)]
    if status == 'Optimal':
        # report steps
        steps = [
            (1, up1, 'd1'), (-1, down1, 'd1'),
            (1, up2, 'd2'), (-1, down2, 'd2')
        ]
        for (sign, v, step) in steps:
            if v.varValue > 0:
                solution.append('-' if sign < 0 else '+')
                solution.append('{} * {}'.format(int(v.varValue), step))
        solution.extend(['=', str(p)])
        print(' '.join(solution))
        return int(num_steps.value())
    elif status in {'Infeasible', 'Not Solved', 'Undefined'}:
        # infeasible or timed out (probably infeasible)
        return -1
    else:
        raise RuntimeError("Problem status was {}".format(status))

print(position(10, 4, 6, 8))
# 10 + 1 * d1 - 1 * d2 = 8
# 2
print(position(10, 4, 6, 9))
# -1
print(position(10, 171, 53, 8))
# 10 - 9 * d1 + 29 * d2 = 8
# 38
print(position(10, 3, 4, 1000000001))
# 10 + 1 * d1 + 250000000 * d2 = 1000000001
# 250000001