def divmod_min(a, b):
    """
    return q,r such that a = qb + r, with minimum |r|
    """
    q, r = divmod(a, b)
    
    # we will want to adjust r if
    #   (|r| > |b/2|), which is equivalent to checking
    #   (|2r| > |b|),
    #   (|r| > |b| - |r|)
    # then using the fact that for python,
    # divmod will give |r| < |b|  and  r,b will have the same sign
    #   (|r| > |b - r|)
    diff = b - r
    if abs(r) > abs(diff):
        q = q + 1 
        r = -diff
    return q,r