You are the manager of a newly opened luxury hotel in the Bahamas!
Each floor of the hotel consists of rooms (numbered ) laid out in a straight line.
However, on your very first day of operations, you are faced with a particularly daunting challenge: accommodating a group of guests (numbered ), who have very specific demands.
You must quickly resolve this situation in order to avoid losing their patronage.
Their demands can be summarised as:
- Every guest has to be put up in a room on the same floor.
- This group wants to book all of the rooms on that floor!
- Any guest may be allocated more than one room.
- If a guest is assigned multiple rooms, these rooms should not be directly adjacent to each other.
Example: If you decide to allocate rooms to guest number , the distribution of rooms cannot be:
. . . 1 1 . . . or . . . 1 1 1 . . . - A particular guest, let us call him/her wants to be allocated at least the last room - the room numbered .
- Guest number wants to be allocated atleast the 1st room - the room numbered .
Thankfully, they do not have any specific demands regarding which room(s) are to be assigned to each of them, as such you are free to come up with a room allocation scheme that follows all of the above constraints.
Remember: Each guest, may be allocated any number of rooms including zero (except , , must be allocated at least 1 room)
Your task is to determine how many such room allocation schemes are possible, since this number may be very large, print it modulo .
Constraints:
Example:
Input: N=4, M=3, X=2
Output: 3
Approach
Java
public class DemandingGuests {public static void main(String args[]) throws Exception {int N = 4;int M = 3;int X = 2;int MOD = 1000000007;long num = 1;boolean less = true;for (int n = 1; n < N - 1; n++) {num = ((num + MOD + (less ? -1 : 1)) * (M - 1)) % MOD;less = !less;}num = ((num + MOD + (less ? -1 : 1)) * (M - 1)) % MOD;if (X == 1)System.out.println(num);elseSystem.out.println((num + MOD + (less ? 1 : -1)) % MOD);}}
No comments:
Post a Comment