Advertisement
Radeen10-_

John and Ann go sign into Codeforces

Feb 11th, 2022
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. John and his wife Ann have decided to go to codeforces. On the first day Ann will do one problem and John - he wants to know how it is working - 0 problem.
  2.  
  3. Let us call a(n) - and j(n) - the number of problems done by Ann - and John - at day n. We have a(0) = 1 and in the same manner j(0) = 0.
  4.  
  5. They have chosen the following rules:
  6.  
  7. On day n the number of problems done by Ann should be n minus the number of problems done by John at day t, t being equal to the number of problems done by Ann herself at day n - 1
  8.  
  9. On day n the number of problems done by John should be n minus the number of problems done by Ann at day t, t being equal to the number of problems done by John himself at day n - 1
  10.  
  11. Whoops! I think they need to lay out a little clearer exactly what there are getting themselves into!
  12.  
  13. Could you write:
  14.  
  15. functions ann(n) and john(n) that return the list of the number of problems Ann/John does on the first n days;
  16. functions sum_ann(n) and sum_john(n) that return the total number of problems done by Ann/John on the first n days
  17.  
  18. ---------example-----------
  19. john(11)  -->  [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6]
  20. ann(6)    -->  [1, 1, 2, 2, 3, 3]
  21.  
  22. sum_john(75)  -->  1720
  23. sum_ann(150)  -->  6930
  24. =====================================================================================================================================****//optimazation should be needed//**
  25.  
  26. #solution:
  27.  #main formula n-a(t) where t is j(n-1)
  28. from functools import lru_cache
  29. import time
  30. @lru_cache ( maxsize = None)
  31. def jon(n):
  32.     if n==0:
  33.         return 0
  34.     else:
  35.         return (n-anna(jon(n-1)))
  36.  
  37.  
  38.     #main formula n-a(t) where t is j(n-1)
  39. @lru_cache ( maxsize = None)
  40. def anna(n):
  41.     if n==0:
  42.         return 1
  43.     else:
  44.         return (n-jon(anna(n-1)))
  45.  
  46. def john(n):
  47.     return [jon(x) for x in range(n)]
  48. def ann(n):
  49.     return [anna(x) for x in range(n)]
  50. def sum_john(n):
  51.     return(sum(john(n)))
  52. def sum_ann(n):
  53.     return(sum(ann(n)))
  54.  
  55. # print(john(num))
  56. # print(sum_john(num))
  57. # print(ann(num))
  58. begin=time.time()
  59. sum_ann(32322)
  60. end = time.time()
  61. print(end-begin)
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement