Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- 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.
- They have chosen the following rules:
- 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
- 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
- Whoops! I think they need to lay out a little clearer exactly what there are getting themselves into!
- Could you write:
- functions ann(n) and john(n) that return the list of the number of problems Ann/John does on the first n days;
- functions sum_ann(n) and sum_john(n) that return the total number of problems done by Ann/John on the first n days
- ---------example-----------
- john(11) --> [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6]
- ann(6) --> [1, 1, 2, 2, 3, 3]
- sum_john(75) --> 1720
- sum_ann(150) --> 6930
- =====================================================================================================================================****//optimazation should be needed//**
- #solution:
- #main formula n-a(t) where t is j(n-1)
- from functools import lru_cache
- import time
- @lru_cache ( maxsize = None)
- def jon(n):
- if n==0:
- return 0
- else:
- return (n-anna(jon(n-1)))
- #main formula n-a(t) where t is j(n-1)
- @lru_cache ( maxsize = None)
- def anna(n):
- if n==0:
- return 1
- else:
- return (n-jon(anna(n-1)))
- def john(n):
- return [jon(x) for x in range(n)]
- def ann(n):
- return [anna(x) for x in range(n)]
- def sum_john(n):
- return(sum(john(n)))
- def sum_ann(n):
- return(sum(ann(n)))
- # print(john(num))
- # print(sum_john(num))
- # print(ann(num))
- begin=time.time()
- sum_ann(32322)
- end = time.time()
- print(end-begin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement