4 seconds
512 megabytes
standard input
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 x�, x+1�+1, …, x+k−1�+�−1 appears in the array;
- consecutive elements of the array differ by at most k� (i.e. |ai−ai−1|≤k|��−��−1|≤� for each i∈[2,n]�∈[2,�]).
You are given n�, x� 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.
The first line contains a single integer t� (1≤t≤501≤�≤50) — the number of test cases.
The only line of each test case contains three integers n�, x� and k� (1≤n,k≤1091≤�,�≤109; 0≤x≤400≤�≤40).
For each test case, print a single integer — the number of fancy arrays of length n�, taken modulo 109+7109+7.
9 25 582 514035484
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[0])
player2.sort(key=lambda x: -x[0])
# 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][0] > player2[j][1]:
j += 1
if j == m:
wins1 += (n – i) # Player1 wins all remaining moves
break
elif player1[i][0] < player2[j][1]:
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)