Harold always boasted about his prowess with numbers. So one day Reese challenged him to a problem. He gave Harold two numbers X and Y and asked him to find out the N^th number of the series which began with X numbers of Y’s and the following elements are equal to the sum of the last X numbers in the series. Help Harold find the N^th number of the series.
Input:
The first line contains a number T , number of test cases.
The next T lines each contain 3 integers X, Y and N separated by single space.
Output:
For each test case print the N^th number of the sequence.
Constraints:
1 ≤ T ≤ 10^6
0 < N ≤ 50
0 < X < 50
0 ≤ Y< 3
We need to parse the problem. It describes a series: starts with X numbers of Y's, and then each following element is sum of the last X numbers. So it's like an X-step linear recurrence: a_i = Y for i=1..X, and for i > X, a_i = sum_{j=i-X}^{i-1} a_j.
We need to compute the N-th number (presumably 1-indexed). Given constraints: T up to 10^6, N ≤ 50, X < 50, Y ∈ {0,1,2}? Actually 0 ≤ Y < 3, so Y can be 0,1,2.
But we have huge number of test cases, but small N, X. So we need to answer quickly, maybe precompute all possibilities? Since T up to 1e6, we need O(1) per test case, or O(log N) at worst. But N ≤ 50, X ≤ 49, we can compute each in O(N * X) which would be up to 2500 per test case, times 1e6 = 2.5e9, too high. So we need more efficient.
We can note recurrence is linear homogeneous with constant coefficients: a_n = sum_{i=1}^X a_{n-i}, with initial conditions a_1..a_X = Y. This is like a generalized Fibonacci. We can compute a_n for given X,Y,N quickly using matrix exponentiation? Since N ≤ 50, we can actually precompute all possible sequences for all X (1..49) and Y (0,1,2) up to N=50. Since total number of (X,Y,N) combos: X from 1 to 49 (but X can be 0? No, 0<X<50, so X from 1 to 49 inclusive? Actually 0<X<50 means X from 1 to 49? 50 not included. So X in [1,49]. Y in {0,1,2}. N in [1,50]. That's at most 49350 = 7350 possibilities. So we can precompute all answers and store in a lookup table. Then for each test case, output table[X][Y][N]. Since T up to 1e6, reading input and outputting quickly is needed but that's okay.
But careful: X could be up to 49, but N can be less than X? For N ≤ X, the answer is simply Y. Because the first X numbers are Y. So for N <= X, answer = Y. For N > X, we need to compute.
We can precompute using DP: For each X, we compute sequence up to 50. Since X is up to 49, we can compute a sliding window sum. That's trivial. So we can generate for each X and Y the sequence a[1..50] quickly.
But also note X can be 0? No.
Thus solution: Precompute answers for all X from 1 to 49, Y from 0 to 2, N from 1 to 50. Use int? But numbers could be large? Let's see: X=49, Y=2, N=50. That's recurrence sum of last 49 numbers. This will grow quickly. Could exceed 32-bit? Let's approximate: It's like linear recurrence with characteristic polynomial x^X - (x^{X-1}+...+1) = 0. The dominant root will be something around 2? Actually for X=1, a_n = a_{n-1} => constant Y. But that's trivial. For X=2, it's Fibonacci: a_n = a_{n-1}+a_{n-2}, starting with Y,Y. So a_n = Y * F_n (with F_1=1? Actually if a_1=Y, a_2=Y, then a_3=2Y, a_4=3Y, a_5=5Y,... So Y times Fibonacci numbers shifted. For N=50, Fibonacci(50) ≈ 1.25e10. Times Y <=2 => ~2.5e10, fits in 64-bit. For X larger, the growth rate is larger? The largest root of x^X = (x^X-1)/(x-1) -> multiply both sides: (x-1)x^X = x^X-1 -> x^{X+1} - x^X - x^X + 1 = 0? Actually better: The recurrence: a_n = sum_{i=1}^X a_{n-i}. The characteristic equation: r^X = r^{X-1} + r^{X-2} + ... + 1. Multiply both sides by (r-1): r^{X}(r-1) = r^X - 1 => r^{X+1} - r^X = r^X -1 => r^{X+1} - 2r^X +1 =0 => r^{X}(r-2) +1 =0. For large X, the dominant root approaches 2. Actually as X→∞, the root approaches 2. So maximum growth factor per step is less than 2. For N=50, max value would be Y * something like < 2^50 ≈ 1e15. Actually 2^50 = 1.12e15. So 64-bit signed can hold up to 9e18, so safe. So we can use 64-bit integers.
Thus we can precompute using unsigned long long or long long.
Implementation:
For each X in 1..49:
For each Y in 0..2:
Initialize array a[1..50] (maybe 0-indexed).
For i=1..X: a[i] = Y.
For i=X+1..50:
Compute sum of last X numbers: sum from i-X to i-1. We can maintain sliding window sum to be efficient but not necessary because X<=49 and total steps <=50, so O(NX) per (X,Y) is fine: 4935049 ≈ 360k operations, negligible.
Or we can do: for i in range X+1..50: a[i] = 0; for j=1..X: a[i] += a[i-j];
Store a[N] in table[X][Y][N] (maybe 3D array: table[50][3][51] (index from 1). We'll allocate as static int64.
But note: X up to 49, we can index from 0 to 49 inclusive ignoring 0. We'll allocate table[50][3][51] (use X as index). Or we could use map? Better array.
Input: T, then T lines each with X Y N.
Output: For each line, print the N-th number.
Edge Cases: Y=0. Then sequence: first X are 0, then each subsequent sum of last X zeros = 0. So entire sequence zeros. So answer is 0. Good.
Now about constraints: T up to 1e6. So we need fast I/O. In C++, use scanf/printf or ios_base::sync_with_stdio(false). In Java, use BufferedReader and PrintWriter. In Python, maybe too slow for 1e6 lines? Python can handle if using sys.stdin.buffer and sys.stdout.write, but 1e6 lines is okay if code is efficient. But Python with loops might be borderline; but we can precompute and then for each test case just lookup and output. 1e6 lines is doable in Python if optimized (reading all lines at once). However the time limit? Not specified, but typical Hackerearth problem might have 1 sec? Probably need fast. I'll implement in C++.
But the problem statement says "Problem statement in native language : http://hck.re/F91Yaz". That link likely is a Hackerearth problem. Many such problems have strict time limits. So C++ safer.
We'll write:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_X = 50; // we'll use index 1..49
const int MAX_Y = 3; // 0,1,2
const int MAX_N = 50;
ll ans[MAX_X][MAX_Y][MAX_N+1]; // ans[X][Y][N], X from 0..49 but ignore 0, Y from 0..2
void precompute() {
for (int X = 1; X <= 49; X++) {
for (int Y = 0; Y < 3; Y++) {
ll seq[MAX_N+1]; // 1-indexed
for (int i = 1; i <= X; i++) seq[i] = Y;
for (int i = X+1; i <= MAX_N; i++) {
ll sum = 0;
for (int j = 1; j <= X; j++) {
sum += seq[i - j];
}
seq[i] = sum;
}
for (int N = 1; N <= MAX_N; N++) {
ans[X][Y][N] = seq[N];
}
}
}
}
int main() {
precompute();
int T;
scanf("%d", &T);
while (T--) {
int X, Y, N;
scanf("%d %d %d", &X, &Y, &N);
printf("%lld\n", ans[X][Y][N]);
}
return 0;
}
Now let's check large X: X=49,Y=2,N=50: compute sum, likely big but fits.
Thus solution is ready.
But we must consider that N can be 0? Constraint says 0 < N ≤ 50, so N≥1.
Thus we are done.
Now potential pitfalls: The problem might have T up to 10^6, but we read input and output, we must ensure not to flush after each output? Using printf and newline is fine, but might be slower. Could use puts? but then need convert number to string. Probably okay.
We can also use write using putchar_unlocked, but not necessary.
Now double-check the sample input and output: Provided sample: 1 line test case: 3 2 7 -> output 34.
All good.
Now we can produce final answer with explanation.
But the user asked: "Write the code in Python". Actually the user didn't specify language, they might want code in any language? The context says "Write the code in Python?" Not sure. In the chat, they didn't specify. They said "Help Harold find the N^th number of the series." They may expect a solution.
But typically they'd expect code in Python? Possibly they want to implement. However the problem link is from Hackerearth, often people ask for solutions in C++ or Python. In the description, there is "SAMPLE INPUT" etc. But they didn't specify language. Since they said "Harold always boasted about his prowess with numbers. So one day Reese challenged him to a problem. He gave Harold two numbers X and Y and asked him to find out the N^th number of the series which began with X numbers of Y’s and the following elements are equal to the sum of the last X numbers in the series. Help Harold find the N^th number of the series." It's a typical programming problem.
Given that the user is asking "Help Harold find the N^th number", they likely want a solution. I can provide explanation and code.
But to be safe, I'll provide both explanation and code in Python and C++? The problem might be from a Python environment? However constraints up to 1e6 T, Python might struggle with 1e6 lines input and output, but if we use fast I/O it could be okay. But 1e6 lines might be near limit in Python, but could pass if optimized and if time limit is generous (maybe 2-3 seconds). Many Hackerearth Python solutions for 1e6 lines can pass if using sys.stdin.buffer.read and splitting into ints. But need to be careful. But we can precompute and then output quickly.
Given that the constraints include T up to 10^6, typical Hackerearth problems with Python may require fast I/O and possibly use PyPy. But I'll provide Python code that uses sys.stdin.buffer.read and precomputation, and join results to print.
Alternatively, I could provide C++ code, as it's safe.
Since the user didn't specify, I'll provide both? Usually ChatGPT responses contain one language. I'll produce Python code, as many users prefer Python. But we need to ensure it's efficient.
Let's design Python solution:
Precompute answers for all X in 1..49, Y in 0..2, N in 1..50. Use nested list or dictionary. We'll store in a 3D list: ans[X][Y][N] with X from 0..49 maybe, but we can ignore index 0.
Use list comprehension: ans = [[[0]*51 for _ in range(3)] for __ in range(50)]? Actually size: 50 x 3 x 51. Index X from 0 to 49; we'll map X directly. But X from 1 to 49, so ans[X][Y][N] works if we allocate 50 entries for X dimension (using index 0 unused). Similarly Y from 0 to 2 inclusive. N from 0 unused.
Precompute: for X in range(1,50): for Y in range(3): seq = [0]* (51) (1-indexed, so we can put seq[0]=0 and fill from 1). For i in range(1, X+1): seq[i]=Y. Then for i in range(X+1,51): seq[i]=sum(seq[i-X:i])? Actually need sum of last X numbers: from i-X to i-1 inclusive. In Python slice: seq[i-X:i] works because i-X and i-1 inclusive. But careful: if using 0-indexed list but with index from 1, we need to adjust. Let's make seq size 51 with index 0..50. We'll fill seq[0] = 0, and seq[1..50]. Then for i in range(X+1,51): seq[i] = sum(seq[i-X:i])? That sums indices i-X to i-1 inclusive, correct. But note that i-X could be 0? Since i >= X+1, i-X >=1, so no problem. So sum(seq[i-X:i]) works. However for large X, slicing sum each time may be O(X), but overall 49350*49 = about 360k sums, fine.
Then assign ans[X][Y][N] = seq[N] for N in range(1,51).
Then read input: using sys.stdin.buffer.read to read all bytes, split into ints. Because T up to 1e6, we have 3*1e6+1 ints ~ 3e6 ints, which is okay.
Then iterate: first int is T, then each three ints: X, Y, N. For each, append str(ans[X][Y][N]) to a list of strings, then after loop, write "\n".join(list). But beware memory: 1e6 strings, each maybe up to 15 chars, so ~15 MB, plus overhead. Could be okay but might be heavy. Alternative is to write to stdout incrementally using sys.stdout.write. But Python's write can be buffered, so we can accumulate in a list and then join is faster. Typically it's recommended for many lines to collect and join. But 1e6 lines, join will create a huge string. That string may be ~15 MB, okay. So collect lines in list and join.
Better: use io.BytesIO to accumulate bytes. But simpler: use sys.stdout.write for each line? That might be slower due to many calls. But with 1e6 calls, it might be okay if using 'write' and maybe flush at end. Actually sys.stdout.write for 1e6 lines might be okay, but many say join is faster. I'd use a list of strings and then "\n".join.
Alternatively, we can use a StringBuilder pattern: out_lines = []; for each test: out_lines.append(str(ans[X][Y][N])); after loop: sys.stdout.write("\n".join(out_lines)). Ensure newline at end maybe add last newline? It's fine.
Now time: precomputation negligible. Parsing ints from bytes: use map(int, data.split())? But that creates a list of all ints, 3e6+1 ints, memory ~ 24 MB? Actually Python int objects are heavy (28 bytes each) so 3e6 ints would be ~84 MB, which may be high but maybe okay? But we risk memory limit. So better to parse iteratively without creating a huge list. Use an iterator over the splitted bytes? data.split() returns list of byte strings, each maybe small, but still many objects (3e6). That's a lot. Each bytes object has overhead. That might be too memory intensive. So we need a more memory-efficient method.
We can read line by line using sys.stdin.buffer.readline in a loop. That's simpler: read T, then for _ in range(T): line = sys.stdin.buffer.readline().split(); X = int(...); Y = int(...); N = int(...). This creates a small list per line and then discards. It also does 1e6 iterations, which is okay in Python? 1e6 line reads might be a bit heavy but likely acceptable in PyPy if optimized. However each readline call might be okay. But we need to ensure that the input reading is fast. Typical competitive programming in Python with 1e6 lines can be done with sys.stdin.buffer.read and splitting into ints, but memory might be high. But 1e6 lines each with 3 ints, if we split into a list of ints, we have 3e6+1 ints; each int is 28 bytes (in CPython) -> about 84 MB, plus list overhead, maybe >100 MB. That could exceed memory limit (often 256 MB). So maybe okay but borderline. Using readline might be slower but uses less memory because each line's small list is freed. But we need to consider time.
Alternatively, we can use sys.stdin.buffer.read().split() and then iterate with index to avoid storing ints? Actually the split returns a list of byte strings, not ints. Converting each to int on the fly still stores the byte strings. The list of byte strings will also be huge: each byte string object overhead. That could be >100 MB as well. So maybe readline is better.
Given typical Hackerearth time limits, Python might struggle with 1e6 lines if using slow methods, but many solutions use sys.stdin.buffer.readline. I'd use that.
But maybe the problem expects C++ more. However the user didn't specify language. Since the conversation is "Write the code in Python?" might be a question? Actually the prompt: "Harold always boasted about his prowess with numbers. So one day Reese challenged him to a problem. He gave Harold two numbers X and Y and asked him to find out the N^th number of the series which began with X numbers of Y’s and the following elements are equal to the sum of the last X numbers in the series. Help Harold find the N^th number of the series." Possibly they want a code solution, not necessarily Python. They might want the answer.
Given that the conversation is from a user requesting help, they might be expecting an explanation and code in any language. I'll assume they want a solution in Python, as many people ask for Python.
I'll provide a Python solution with fast I/O and precomputation.
Now I need to be careful about indices. In Python, we can use a dict or list.
Implementation plan:
MAX_X = 50 (though we'll use indices 0-49)
MAX_Y = 3
MAX_N = 50
ans = [[[0]*(MAX_N+1) for _ in range(MAX_Y)] for __ in range(MAX_X)] # ans[X][Y][N]
Note: X from 0 to 49 inclusive, but we will fill for X in 1..49.
Precompute:
for X in range(1, 50):
for Y in range(3):
seq = [0]*(MAX_N+1)
for i in range(1, X+1):
seq[i] = Y
for i in range(X+1, MAX_N+1):
# sum last X
s = 0
for j in range(1, X+1):
s += seq[i - j]
seq[i] = s
for N in range(1, MAX_N+1):
ans[X][Y][N] = seq[N]
But note: we have ans indexed by X, Y, N. In lookup: ans[X][Y][N].
Then reading:
import sys
def main():
import sys
data = sys.stdin.buffer.read().split()
# But we don't want to create huge list. Actually we can use iterator.
# Use map with iter? Let's see.
# Better: read first integer, then loop reading three ints each.
# But reading entire file and splitting may still create many byte strings. However it's common for such size.
# Let's adopt reading line by line to avoid memory.
# We'll use readline.
But I'm not sure which is best. Let's think: 1e6 lines, each line about 5-10 bytes, total input about 5-10 MB. Splitting into bytes objects yields ~3e6 tokens (each token a small bytes). Each bytes object overhead maybe ~33 bytes? That would be about 100 MB, plus list overhead maybe 24 MB. So ~124 MB. That's okay if memory limit is 256 MB, but might be high but often acceptable. However we also have ans array (maybe small). So it's okay. However Python's memory fragmentation could push it over. I'd prefer to use an iterator over the splitted tokens, but that still holds the list.
Alternatively we can use sys.stdin.buffer.readline in a loop: each line read and split. That yields temporary lists that are freed. That might be slower due to function calls but uses less memory. But 1e6 calls to readline may be okay. I've done such in Python with PyPy and passed.
Given that I'll provide a Python solution, I'll use readline for clarity and safety.
Code:
import sys
def main():
pre = [[[0]*51 for _ in range(3)] for __ in range(50)]
# pre[X][Y][N] (X from 0..49, Y from 0..2, N from 0..50)
for X in range(1, 50):
for Y in range(3):
seq = [0]*51
for i in range(1, X+1):
seq[i] = Y
for i in range(X+1, 51):
s = 0
for j in range(1, X+1):
s += seq[i-j]
seq[i] = s
for N in range(1, 51):
pre[X][Y][N] = seq[N]
code
input = sys.stdin.buffer.readline
T_line = input()
while T_line.strip() == b'':
T_line = input()
T = int(T_line)
out_lines = []
for _ in range(T):
line = input()
while line.strip() == b'':
line = input()
X_str, Y_str, N_str = line.split()
X = int(X_str)
Y = int(Y_str)
N = int(N_str)
out_lines.append(str(pre[X][Y][N]))
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()
But note: pre is sized 50 for X dimension, but X can be up to 49, so pre[49] exists. Good.
We might also include for X=0? Not needed.
Edge Cases: X, Y, N are within ranges as per constraints; no validation needed.
But wait: Y can be 0,1,2 but the input could have Y as 0? Yes. So pre[X][0][N] is computed.
Now test with sample:
Input:
1
3 2 7
Should output 34.
Now also note that X may be 1? pre[1][Y][N] sequence: a[1]=Y, for i>1: a[i] = sum of last 1 => a[i] = a[i-1] = Y. So all Y. So answer Y. Works.
Now we need to ensure that pre[X][Y][N] for N up to 50 computed correctly.
Potential performance: The triple nested loops for precomputation: 49 * 3 * (up to 50) * (sum up to 49) ~ 49350*25? Actually sum of X for each X? For each X we do loops. But it's okay.
Now about reading T: there may be trailing newlines. We handle empty lines with while loops, but we can assume no empty lines. So we can simplify:
T = int(sys.stdin.readline())
for _ in range(T):
X, Y, N = map(int, sys.stdin.readline().split())
...
But using sys.stdin.buffer.readline is faster.
Now the output: join may produce a long string. That's okay.
Now alternative: use write for each line: maybe slower but fine. But join is fine.
Now ensure we include newline at end? join puts newline between each, and no trailing newline. Usually okay.
Now final answer: Provide explanation and code.
But the user said: "Write the code in Python?" Actually the conversation says: "Harold always boasted about his prowess with numbers. So one day Reese challenged him to a problem. He gave Harold two numbers X and Y and asked him to find out the N^th number of the series which began with X numbers of Y’s and the following elements are equal to the sum of the last X numbers in the series. Help Harold find the N^th number of the series." That's just the problem statement. So they might be asking for a solution. I'll respond with explanation and code.
Thus I will output the solution in Python as it is a common language.
But also note that the problem has a link: http://hck.re/F91Yaz, which might be a Hackerrank/Hackerearth problem. Many people want Python code.
Case: X=2 Y=1 N=5 -> series: 1,1,2,3,5 => 5. Precomputation: should be 5.
Check manually: for X=2, Y=1: pre[2][1][5]? In code, we compute seq[1]=1, seq[2]=1, then i=3: sum seq[2]+seq[1]=2, i=4: sum seq[3]+seq[2]=3, i=5: sum seq[4]+seq[3]=5. Works.
Now case: X=4 Y=2 N=6? Let's compute: first 4: 2,2,2,2. a5 = sum last 4 = 2+2+2+2=8. a6 = sum a2..a5 = 2+2+2+8=14. So ans should be 14. Check code.
Now X=49 Y=2 N=50: compute, should be some big number. We'll trust.
Now we need to consider that X might be > N? Then ans = Y. In our seq, we filled seq[1..X]=Y, so seq[N]=Y if N<=X. So correct.
Now we need to ensure that we computed for all N up to 50, inclusive. So pre[X][Y][N] is defined for N=50.
Now about indexing: pre[49][2][50] will exist.
Now about reading T: T up to 10^6, reading lines with readline in a loop 1e6 times is okay but might be near time limit in Python. However we can further optimize by reading all at once and using map with iter. Let's analyze memory: The input is about 10 MB, reading all and splitting into a list of bytes may be okay. Then we can iterate through the ints. Since we have 3 million tokens plus 1, the memory consumption might be high but still within typical 256 MB? Let's approximate: each token as bytes object: for "123" it's b'123', overhead maybe 33 bytes plus length. So 3e6 tokens * 33 bytes = 99 MB. The list overhead: 3e6 * 8 bytes for pointer? Actually Python list stores pointers to PyObject, each pointer 8 bytes. So 24 MB for list. So total ~123 MB. Plus other objects, maybe ~150 MB. That's acceptable if memory limit is 256 MB. However Python's memory fragmentation may push it higher but likely still within limit. But also we have precomputed table ~ small. So it could be okay. But reading line by line might be simpler and use less memory, but the overhead of 1e6 readline calls might be high.
We can test: reading all lines and splitting might be faster because it's a single read and split. I've seen many Python solutions for large input use sys.stdin.buffer.read().split() and then iterate. That is considered efficient. But we must convert to ints; we can convert on the fly using map(int, ...) but that would create another list of ints which duplicates memory. So we should iterate over the byte strings and convert as we go.
We can do:
data = sys.stdin.buffer.read().split()
it = iter(data)
T = int(next(it))
out = []
for _ in range(T):
X = int(next(it))
Y = int(next(it))
N = int(next(it))
out.append(str(pre[X][Y][N]))
sys.stdout.write("\n".join(out))
That way we don't create a list of ints, just iterate over the list of bytes strings, converting one by one. The list of bytes strings is still there, but that's the main memory. Could we avoid storing that? We can use map(int, data) to get an iterator of ints? Actually map returns iterator that will convert on the fly. But data is list of bytes. Using map(int, data) would produce an iterator that yields ints; but we need to skip first for T. But we can do:
ints = map(int, sys.stdin.buffer.read().split())
T = next(ints)
...
This still creates the list of bytes? Actually sys.stdin.buffer.read().split() returns a list, so it's stored. The map will create an iterator over that list. So memory for list of bytes remains. So we cannot avoid that. But we can read lines? Actually reading all lines with .readlines() would also create a list of lines, similar memory. So reading all at once and splitting may be the fastest.
We could use a generator that reads in chunks and yields ints without storing all tokens. That's more complex.
Given the time, it's likely that the test input is such that Python solution with read().split() passes. Many Hackerearth Python solutions for 1e6 lines use read().split().
I'll go with that.
But note: In Python, using read().split() will load the entire input into memory as a single string (which is about 10 MB), then split into list of bytes objects (another overhead). That might be okay.
Let's decide: I'll provide both options? I'll provide the simpler readline version for clarity and note that for large T, one may need faster I/O. But typical Python solution on Hackerearth uses sys.stdin.buffer.read.
Given the audience, I'll provide the read().split() version, as it's common.
Thus final code:
import sys
def main():
MAX_X = 50
MAX_Y = 3
MAX_N = 50
ans = [[[0](MAX_N+1) for _ in range(MAX_Y)] for __ in range(MAX_X)]
for X in range(1, MAX_X):
for Y in range(MAX_Y):
seq = [0](MAX_N+1)
for i in range(1, X+1):
seq[i] = Y
for i in range(X+1, MAX_N+1):
total = 0
for j in range(1, X+1):
total += seq[i - j]
seq[i] = total
for N in range(1, MAX_N+1):
ans[X][Y][N] = seq[N]
data = sys.stdin.buffer.read().split()
it = iter(data)
T = int(next(it))
out = []
for _ in range(T):
X = int(next(it))
Y = int(next(it))
N = int(next(it))
out.append(str(ans[X][Y][N]))
sys.stdout.write("\n".join(out))
if name == "main":
main()
But note: ans is defined as list of size 50 for X. X ranges from 1 to 49 inclusive. ans[0] is unused. So ans[X] for X=49 exists. Good.
We might also need to handle X maybe 0? Not possible.
Now test with sample: We'll simulate.
Potential bug: In the loops, we used range(1, MAX_X) -> 1..49 inclusive. That's correct because MAX_X=50, range(1,50) gives 1..49. Good.
Now note: For Y, range(MAX_Y) gives 0,1,2. Good.
Now for N, we stored up to MAX_N=50, inclusive.
Now the code uses ans[X][Y][N] directly. But Y might be 2, okay.
Now we need to be careful about int overflow? Python int is arbitrary precision, so fine.
Now final answer: Provide explanation and code.
But the problem statement includes a sample output. So answer should produce that.
Now I'll produce final message with explanation and code. We need to compute the N-th term of a sequence defined by:
First X terms are all Y.
For i > X, term i = sum of the previous X terms.
Given the constraints (T up to 10^6, X, N ≤ 50, Y ≤ 2) we can precompute all answers for X=1..49, Y=0..2, N=1..50 and answer each query in O(1).
Precomputation
For each X (1 to 49) and Y (0,1,2):
Initialize an array a[1..50] with a[1..X] = Y.
For i from X+1 to 50: a[i] = sum_{j=1..X} a[i-j].
Store a[N] in a table ans[X][Y][N].
Then for each test case, simply output ans[X][Y][N].
Complexity
Precomputation: about 49·3·50·X ≈ 360k operations → negligible.
Per test: O(1) lookup.
Input/Output: fast reading/writing (using sys.stdin.buffer.read and sys.stdout.write) to handle up to 10^6 cases.
Python Implementation
python
import sys
def precompute():
MAX_X = 50 # X goes up to 49
MAX_Y = 3 # Y = 0,1,2
MAX_N = 50 # N up to 50
ans = [[[0] * (MAX_N + 1) for _ in range(MAX_Y)] for __ in range(MAX_X)]
for X in range(1, MAX_X): # 1..49
for Y in range(MAX_Y): # 0..2
seq = [0] * (MAX_N + 1) # 1‑based
# first X terms are Y
for i in range(1, X + 1):
seq[i] = Y
# fill the rest up to 50
for i in range(X + 1, MAX_N + 1):
total = 0
for j in range(1, X + 1):
total += seq[i - j]
seq[i] = total
# store all N values
for N in range(1, MAX_N + 1):
ans[X][Y][N] = seq[N]
return ans
def main():
ans = precompute()
data = sys.stdin.buffer.read().split()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
X = int(next(it))
Y = int(next(it))
N = int(next(it))
out_lines.append(str(ans[X][Y][N]))
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
main()