Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. with open('input.txt') as f:
  2.     n = int(f.readline())
  3.     a = [int(i) for i in f.readline().split()]
  4.  
  5. calculated = dict()
  6.  
  7. def rec(position):
  8.     if position == 0:
  9.         return 0
  10.     if position == 1:
  11.         return 1
  12.     if position == 2:
  13.         return 2
  14.     i = 3
  15.     last1 = 2
  16.     last2 = 1
  17.     last3 = 0
  18.     while i <= position:
  19.         result = last1 + last3
  20.         last3 = last2
  21.         last2 = last1
  22.         last1 = result
  23.         calculated[i] = result
  24.         i+=1
  25.     return result
  26.  
  27. with open("output.txt", "w") as f:
  28.     for i in range(len(a)):
  29.         if a[i] in calculated:
  30.             if i != len(a)-1:
  31.                 f.write(str(calculated[a[i]])+' ')
  32.             else:
  33.                 f.write(str(calculated[a[i]]))
  34.         else:
  35.             if i != len(a)-1:
  36.                 f.write(str(rec(a[i]))+' ')
  37.             else:
  38.                 f.write(str(rec(a[i])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement