Tile Permutation Paradox

3.2

4 votes
Algorithms, Dynamic Programming, Counting and Arrangements
Problem

Once upon a time in the quaint village of Turlington, there lived a talented artisan named Ella. Ella had a unique skill – she could transform any floor into a mesmerizing mosaic using special tiles. These magical tiles were of size 1×X, and Ella's artistic flair knew no bounds.

One day, the villagers gathered around Ella's workshop, curious about the secrets behind her enchanting tile arrangements. Ella, always happy to share her knowledge, decided to turn this curiosity into a challenge. She introduced the villagers to a puzzle: The problem of finding the absolute difference between the number of distinct ways and the number of distinct ordered ways to tile a floor of size N×X . The twist in the tale was that the tiles could be rotated in any direction, meaning a 1×X tile could also become an X×1 tile.

The villagers, eager to test their problem-solving skills, embarked on a series of Q queries. In each query, they provided two numbers, N and X, representing the dimensions of the floor and the magical tiles, respectively. Ella emphasized that the answers should be calculated modulo 109+7 since the answer can be very large.

Distinct Ways:

  • Definition: "Distinct ways" refer to the count of unique ordered arrangements or permutations of tiles on the floor, considering both the orientation and the order of the tiles.
  • Example: Suppose N=4 and X=2. The distinct ways will be [1×X,1×X,1×X,1×X],[1×X,1×X,X×X],[1×X,X×X,X×1],[X×X,1×X,1×X],[X×X,X×X].

Distinct Ordered Ways:

  • Definition: "Distinct ordered ways" refer to the count of unique arrangements or combinations of tiles on the floor, considering the orientation of the tiles but not the order.
  • Example:  Suppose N=4 and X=2. The distinct ordered ways will be [1×X,1×X,1×X,1×X],[1×X,1×X,X×X],[X×X,X×X]. In ordered ways, the set of tiles arrangements will be added but not the order of the tiles.

Input format

  • The first line contains an integer Q representing the number of queries.
  • For each of the Q queries, there are two space-separated integers N and X.

Output format

  • For each query, output a single integer representing the absolute difference between the number of distinct ways and the number of distinct ordered ways to tile the floor, considering the ability to rotate the tiles.
  • Since the answer can be very large, output the result modulo 109+7.

Constraints

1Q1001N1042X106

 

Sample Input
4
6 4
5 3
10 4
11 2
Sample Output
2
2
11
138
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the query 1:

N = 6 and X = 4

Number of distinct ways = 4

No of  distinct ordered ways = 2 (because 2nd ,3rd and 4th are considered as same arrangement)

So, the answer is absolute(4 - 2) = 2.

Editor Image

?