Demanding Guests

You are the manager of a newly opened luxury hotel in the Bahamas!
Each floor of the hotel consists of N rooms (numbered  1N ) 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  M guests (numbered 1M),  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 N 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 3 rooms to guest number 1, the distribution of rooms cannot be:
    . . . 1 1 . . .  or . . . 1 1 1 . . . 
  • A particular guest, let us call him/her X wants to be allocated at least the last room - the room numbered N.
  • Guest number 1 wants to be allocated atleast the 1st room - the room numbered 1.

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 X , 1, 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 109+7.

Constraints:
3<=N<=105
2<=M<=105
1<=X<=M

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);
        else
            System.out.println((num + MOD + (less ? 1 : -1)) % MOD);
    }

}


No comments:

Post a Comment