# [SOLUTION] Fancy Arrays SOLUTION CODEFORCES

F. Fancy Arrays
time limit per test

4 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Let’s call an array a of n non-negative integers fancy if the following conditions hold:

• at least one from the numbers xx+1�+1, …, x+k1�+�−1 appears in the array;
• consecutive elements of the array differ by at most k (i.e. |aiai1|k|��−��−1|≤� for each i[2,n]�∈[2,�]).

You are given nx and k. Your task is to calculate the number of fancy arrays of length n. Since the answer can be large, print it modulo 109+7109+7.

Input

The first line contains a single integer t (1t501≤�≤50) — the number of test cases.

The only line of each test case contains three integers nx and k (1n,k1091≤�,�≤1090x400≤�≤40).

Output

For each test case, print a single integer — the number of fancy arrays of length n, taken modulo 109+7109+7.

Example
input

Copy
4
3 0 1
1 4 25
4 7 2
1000000000 40 1000000000
output

Copy
9
25
582
514035484

Note

In the first test case of the example, the following arrays are fancy:

• [0,0,0][0,0,0];
• [0,0,1][0,0,1];
• [0,1,0][0,1,0];
• [0,1,1][0,1,1];
• [0,1,2][0,1,2];
• [1,0,0][1,0,0];
• [1,0,1][1,0,1];
• [1,1,0][1,1,0];
• [2,1,0][2,1,0].

SOLUTION

# Function to calculate the number of Monocarp’s starting moves that result in a win, draw, and loss
def calculate_moves_outcome(n, ax, ay, m, bx, by):
# Create a list of tuples (attack, defense)
player1 = [(ax[i], ay[i]) for i in range(n)]
player2 = [(bx[i], by[i]) for i in range(m)]

# Sort the lists by attack values in descending order
player1.sort(key=lambda x: -x)
player2.sort(key=lambda x: -x)

# Initialize counters for wins, draws, and losses
wins1, draws, wins2 = 0, 0, 0
j = 0 # Index for player2

for i in range(n):
while j < m and player1[i] > player2[j]:
j += 1
if j == m:
wins1 += (n – i) # Player1 wins all remaining moves
break
elif player1[i] < player2[j]:
draws += 1 # Player2 wins this move
else:
j += 1 # Player1 and Player2 draw this move

for i in range(j, m):
wins2 += 1 # Player2 wins all remaining moves

return wins1, draws, wins2

# Read the number of test cases
t = int(input())

# Iterate through each test case
for _ in range(t):
n = int(input())
ax = list(map(int, input().split()))
ay = list(map(int, input().split()))
m = int(input())
bx = list(map(int, input().split()))
by = list(map(int, input().split()))

# Calculate the number of wins, draws, and losses for Monocarp
wins1, draws, wins2 = calculate_moves_outcome(n, ax, ay, m, bx, by)

# Print the results for this test case
print(wins1, draws, wins2)