简体   繁体   中英

How can I change my code so that the “time limit exceeded” error message does not show up?

I am doing Codeforces problem 732A. How can I change my solution so that there is no error message that claims "Time limit exceeded"? By the way, I am a beginner coder who has started only but a few weeks ago, so please try to be simple with your help:)

My code:

732A

k,r = list(map(int, input().split()))

t = 0

while (((t * 10) + r) % k):= 0: t = t + 1

print ((((t * 10) + r) // k))

I was able to find the problem, but you should probably link it at least next time. The way you are solving it is correct but involves too many iterations of your while loop which is causing the time limit error. Instead of doing (t*10)+r until you find a multiple of k, you could do something like

k,r = list(map(int, input().split()))
temp = k
num = 1
while temp%10 and (temp-r)%10:
    temp+=k
    num+=1

print(num)

The way this works is it finds the first multiple of k (or multiple of k minus r) that is divisible by 10, which should be faster than your solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM