Problem 179941 · hard · Phase 01 Prerequisites & Setup

The Tilted Cup Tower

lists · nested lists · simulation

At a party, paper cups are stacked in a triangle with rows rows: row r (counting from 0 at the top) has r + 1 cups, numbered 0 to r from the left. Cup c of row r rests on cups c and c + 1 of row r + 1. Every cup holds at most capacity millilitres.

Someone pours amount millilitres into the top cup, all at once. Whenever a cup receives more than capacity in total, it keeps capacity and the excess e runs over its rim. The cups lean slightly, so the cup below on the left receives e - e // 2 and the cup below on the right receives e // 2. Excess from the bottom row runs onto the table.

Write cup_tower(rows, capacity, amount) that returns [full, spilled]: the number of cups that end up holding exactly capacity, and the millilitres that reach the table. With rows = 0 there are no cups and everything is spilled.

Examples

Input:  rows = 3, capacity = 4, amount = 20
Output: [4, 0]
Explanation: the top cup passes 8 and 8 down. Each cup of row 1 keeps 4 and passes 2 and 2 down,
so row 2 holds 2, 4 and 2.

Input:  rows = 2, capacity = 1, amount = 10
Output: [3, 7]
Explanation: the top passes 5 left and 4 right. They pass on 4 (as 2 and 2) and 3 (as 2 and 1).

Input:  rows = 3, capacity = 2, amount = 11
Output: [5, 0]

Constraints

  • 0 <= rows <= 60, 1 <= capacity <= 10**6
  • 0 <= amount <= 10**18

Goals

  • See that only the total each cup receives matters, not the order drops arrive in
  • Pass overflow down one row at a time with careful indices
  • Keep every amount an exact integer
Starting Python…