Jin's answer is good enough
Answer in three lines with recursive functions is preferred and sufficient.
Direct functions are pure math and labor intense to me. I am bad at math.
My initiative is more related to the way of thinking –
“if A and B are both smart, B can just sit back and ‘give up/waive’ ALL HIS RIGHTS!” Understand this sentence is the same important as getting the answer.
“如果A B和阁下一样聪明,B 从头到尾无需动用其优先权。 “