Hilbert's Hotel
Time limit1.5sMemory limit1024 MB
Simulate Hilbert's Hotel as guests arrive in finite or infinite groups, and answer which group occupies a room or which room holds a given index of a group.
- Level
Hard8 of 10
- Topics
- Math, Implementation, Binary search, Combinatorics
- Solved
- No attempts yet
Problem
Hilbert's Hotel has infinitely many rooms, numbered 0, 1, 2, . At most one guest occupies each room. Since people tend to check in as groups, the hotel keeps a group counter variable .
Hilbert's Hotel opened today. Soon after, infinitely many people arrived at once, filling every room. All guests received group number 0, and was set to 1.
Ironically, the hotel can accept more guests even though every room is full.
- If () people arrive at the hotel, then for each room number , the guest in room moves to room . The new guests then fill all rooms from 0 to .
- If infinitely many people arrive at the hotel, then for each room number , the guest in room moves to room . The new guests then fill all rooms with odd numbers.

You must write a program that processes the following queries.
1 k- If , then people arrive at the hotel. If , then infinitely many people arrive. Assign group number to the new guests, then increment by 1.2 g x- Find the -th smallest room number occupied by a guest with group number . Output that value modulo , followed by a newline.3 x- Output the group number of the guest in room , followed by a newline.
Input
The first line contains an integer (), the number of queries. Each of the next lines contains a query. All numbers in the queries are integers.
- For
1 kqueries, . - For
2 g xqueries, , , and at least guests have group number . - For
3 xqueries, .
Output
Process all queries and output as required. It is guaranteed that the output is not empty.
Hint
If you know about cardinals, assume that "infinite" refers only to "countably infinite." If you do not, you do not have to worry about it.