Data Structures & AlgorithmsVery hardCodeforces Div 2 D · ICPC regional
Consider this recursive function:
Consider this recursive function:
def f(n):
if n <= 1:
return
f(n - 2)
if n % 3 == 0:
f(n - 2)How many times is f called in total, including the initial call, when f(8) is made?- A. 17
- B. 31
- C. 8
- D. 7
Correct answer: C. 8
Each call makes one call to f(n − 2) and a second call only when n is divisible by 3; counting every invocation gives 8. Forgetting the divisibility test gives 31, and counting only the calls that are not the initial one gives 7.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- An undirected graph has the vertices J, K, M, O, P, Y and the edges P–M, P–Y, P–O, P–K, P–J, M–Y, M–O, Y–K, O–K. What is its chromatic…
- A directed graph has the vertices B, F, I, S and the arcs I→F, I→S, F→I, F→B, S→I, S→F, S→B, B→F, B→S. Count the directed Hamiltonian…
- An undirected graph has the vertices L, O, Q, Y, Z and the edges L–Z, O–Z, O–Y, Q–O, Q–Y, L–Y, L–Q, Y–Z. What is the minimum number of…
- An undirected graph has the vertices C, E, L, M, T and the edges L–E, L–T, M–C, E–C, C–T. In how many ways can each vertex be given one of…
- A directed acyclic graph has the vertices A, F, H, I, K, Q, R and the edges A→H, Q→A, Q→F, Q→H, R→F, F→H, F→K, F→I. A topological order…
- A tree has the vertices A, E, H, P, R, S, T, U, W, Y and the edges H–T, T–E, T–A, E–P, T–Y, A–R, Y–S, R–U, U–W. A set D dominates the tree…