You are viewing a single comment's thread. Return to all comments →
import sys import math
def closest_multiple(a, b, x): value = a ** b # this will be a float if b is negative div = value / x
lower = math.floor(div) upper = lower + 1 lower_mult = lower * x upper_mult = upper * x diff_lower = abs(value - lower_mult) diff_upper = abs(value - upper_mult) if diff_lower < diff_upper: return round(lower_mult) elif diff_lower > diff_upper: return round(upper_mult) else: return round(min(lower_mult, upper_mult)) # tie case
def main(): input = sys.stdin.read data = input().split()
T = int(data[0]) idx = 1 results = [] for _ in range(T): a = int(data[idx]) b = int(data[idx + 1]) x = int(data[idx + 2]) idx += 3 res = closest_multiple(a, b, x) results.append(str(res)) print("\n".join(results))
main()
Seems like cookies are disabled on this browser, please enable them to open this website
Closest Number
You are viewing a single comment's thread. Return to all comments →
import sys import math
def closest_multiple(a, b, x): value = a ** b # this will be a float if b is negative div = value / x
Fast input reading
def main(): input = sys.stdin.read data = input().split()
main()