Блог пользователя Kefrov

Автор Kefrov, история, 7 месяцев назад, По-английски

Hello friends, help me figure out why I'm getting a Runtime error on test 6 for 1883E - Look Back. I think my solution is mathematically correct however python or the log2 function may have something to do with this.

Here's my submission : 229312603

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
7 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

This feels like an integer overflow. Maybe the variable “last” got too big

»
7 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

It can be overflow.

from math import log2, ceil
 
for test in range(int(input())):
    n = int(input())
    a = [int(x) for x in input().split()]
 
    res = 0
    last = a[0]
    for i in range(1, n):
        c = a[i]
        p = 0
        if c < last:
            p = ceil(log2(last / c))
            res += p
        last = c * (2 ** p) //This line
 
    print(res)

In marked line you are multipling c by number that can be really big(There is some if-s to fix this).