#P13207. [GCJ 2016 Finals] Family Hotel

    ID: 15076 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划 DP2016概率论Google Code Jam

[GCJ 2016 Finals] Family Hotel

题目描述

You run a hotel with N\mathbf{N} rooms arranged along one long corridor, numbered from 1 to N\mathbf{N} along that corridor. Your guests are big families, and every family asks for exactly two adjacent rooms when they arrive. Two rooms are adjacent if their numbers differ by exactly 1.

At the start of the day today, your hotel was empty. You have been using the following simple strategy to assign rooms to your guests. As each family arrives, you consider all possible pairs of adjacent rooms that are both free, pick one of those pairs uniformly at random, and assign the two rooms in that pair to the family. New families constantly arrive, one family at a time, but once there are no more pairs of adjacent rooms that are both free, you turn on the NO VACANCY sign and you do not give out any more rooms.

Given a specific room number, what is the probability that it will be occupied at the time that you turn on the NO VACANCY sign?

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} lines follow. Each line contains two numbers: the number of rooms N\mathbf{N} and the room number K\mathbf{K} that we are interested in.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the sought probability computed modulo 109+710^9 + 7, which is defined precisely as follows. Represent the probability that room K\mathbf{K} is occupied as an irreducible fraction pq\frac{p}{q}. The number yy then must satisfy the modular equation y×qp(mod109+7)y \times q \equiv p \pmod{10^9 + 7}, and be between 0 and 109+610^9 + 6, inclusive. It can be shown that under the constraints of this problem such a number yy always exists and is uniquely determined.

4
3 1
3 2
4 1
4 2
Case #1: 500000004
Case #2: 1
Case #3: 666666672
Case #4: 1

提示

Sample Explanation

In sample case #3, there are four rooms and we are looking for probability that the first room is occupied. When the first family arrives, there are 3 possible situations, each with probability 1/31/3: occupy rooms 1+2,2+31+2, 2+3 or 3+43+4. In the first situation, the first room is already occupied and will stay occupied. In the second situation, the first room is free and no more families can be accommodated, so it will stay free. Finally, in the third situation, the next arriving family will definitely get rooms 1+21+2, and thus the first room will be occupied. The probability that the first room is occupied is thus 2/32/3, and the answer is 666666672666666672, since $(666666672 \times 3) \mod 1000000007 = 2 \mod 1000000007$.

The probability for sample case #1 is 1/21/2, and for sample cases #2 and #4 it is 11.

Limits

  • 1T1001 \leqslant \mathbf{T} \leqslant 100.
  • 1KN1 \leqslant \mathbf{K} \leqslant \mathbf{N}.

Small dataset (10 Pts, Test Set 1 - Visible)

  • 2N1042 \leqslant \mathbf{N} \leqslant 10^{4}.

Large dataset (20 Pts, Test Set 2 - Hidden)

  • 2N1072 \leqslant \mathbf{N} \leqslant 10^{7}.