We need to solve: given an arithmetic progression (AP) such that the 3rd term (a+2d) and the 3rd last term and the sum are given. Actually, they said "Arithmetic and geometric Progressions are 2 of the well known progressions in maths." But the problem statement says: "Arithmetic progression (AP) is a set in which the difference between 2 numbers in constant. for eg, 1,3,5,7,9....In this series the difference between 2 numbers is 2." So they focus on AP. And then: "The task here is very simple indeed. You will be given the 3rd term , 3rd last term and the sum of the series. You need to print the length of the series & the series itself." So it's about AP.
Given: third term (T3 = a + 2d), third last term (T_{n-2} = a + (n-3)d) and sum S = n/2 * (2a + (n-1)d).
We need to find integer n, a, d (positive integers? Actually they say all series elements are positive integers, and constraints: all numbers (given? maybe the terms and sum) are less than 10^16. And series will have at least 7 elements. So n >=7.
We need to output n and the series (a, a+d, ..., a+(n-1)d).
Given: T3 = A, T_{n-2} = B, S = C (say). Let's denote:
a + 2d = A ... (1)
a + (n-3)d = B ... (2)
Subtract (2)-(1): (n-5)d = B - A => d = (B - A) / (n-5). Since d must be positive integer? The problem says all series elements are positive integers, but doesn't explicitly say d is integer? Since it's an AP with integer terms, d must be integer (if a is integer). Given that elements are positive integers, then a and d are integers (since a is integer, a+d integer => d integer). So d must be integer, and n integer.
Also sum: S = n/2 * (2a + (n-1)d) = n/2 * ( (a + (n-1)d) + a ) = n/2 * (first + last). But we can also express a from (1): a = A - 2d.
Plug into (2): (A - 2d) + (n-3)d = A + (n-5)d = B => (n-5)d = B - A. So that's consistent.
Then sum: S = n/2 * (2a + (n-1)d) = n/2 * (2(A - 2d) + (n-1)d) = n/2 * (2A - 4d + (n-1)d) = n/2 * (2A + d(n-1-4)) = n/2 * (2A + d(n-5)). But (n-5)d = B - A, so d(n-5) = B - A.
Thus S = n/2 * (2A + (B - A)) = n/2 * (A + B).
So S = n * (A + B) / 2.
That's a neat simplification: sum = n * (A + B)/2.
Proof: Because sum = n/2 (first + last) but A = a+2d, B = a+(n-3)d, then first = a, last = a+(n-1)d, but we can also relate: first+last = (a+2d)+(a+(n-3)d) - (2d) +? Actually compute: (A + B) = (a+2d)+(a+(n-3)d)=2a+(n-1)d = first+last? Wait first+last = a + (a+(n-1)d) = 2a+(n-1)d. So indeed A+B = 2a+(n-1)d = first+last. So sum = n/2 * (first+last) = n/2 * (A+B). Yes. So we have:
S = n * (A+B) / 2.
So given A, B, S, we can compute n = 2S / (A+B). Since all are integers, n must be integer and (A+B) divides 2S. Also n >=7.
Then we can compute d = (B - A)/(n-5). And a = A - 2d.
But we must verify that all terms are positive integers, and given constraints, these should hold.
So approach: for each test case, read A, B, S (all integers). Compute numerator = 2S, denominator = (A+B). But note: A+B could be zero? Since all terms positive, A and B positive, so A+B >0. So n = 2S / (A+B). However, we must check that 2*S is divisible by (A+B). According to problem, it should be for valid input.
Then compute n, check n >=7, else maybe invalid? But given constraints, should be valid.
Then compute d_numer = B - A, d_denom = n-5. Since n>=7 => n-5>=2. Also B >= A? Not necessarily: could be decreasing AP? Problem statement gives example 1..10 increasing. They say "all series elements are positive integers". They don't say increasing, but typically AP could be decreasing if d negative? But they gave example increasing; and they say "the difference between 2 numbers is constant" but could be negative. However they say "positive integers" for elements, and if d negative, then after some point terms may become negative? But they said all series elements are positive integers, so if d negative, the sequence would be decreasing; but still could be positive if a >0 and a+(n-1)d>0. Could be possible. But in the relation (B - A)/(n-5) = d, if d negative then B < A. But is that allowed? The problem statement didn't say non-negative d. However they gave all numbers less than 10^16, and "positive integers" for series elements. So it could be decreasing. But we need to decide. Let's check typical problems: Usually they assume increasing AP with positive d. But not necessarily. But given that sum and terms are positive, both possibilities exist. However, we need to output the series. The series will be increasing if d>0, decreasing if d<0. Do we have to choose? The input uniquely determines n and d, so whichever sign comes out. I think we should accept any integer d (including negative) as long as terms are positive integers.
But we need to be careful: d must be integer because terms are integers. So we compute d = (B - A) / (n-5) and check divisible.
Then compute a = A - 2d. Check a > 0 and that last term = a+(n-1)d >0.
Then output n and the series a + i*d for i=0..n-1.
But we also must consider that there might be multiple solutions? Let's verify uniqueness. Given A, B, S, we derived n = 2S/(A+B). That's forced. Then d = (B-A)/(n-5). So unique. So only one possible AP.
Edge Cases: Could n-5 be 0? That would require n=5, but n>=7 so not possible.
But we should also check that the derived a and d produce exactly the given A and B and S. Since we used the relation that sum = n/2 * (A+B), if that holds, then automatically? Actually we derived n from that, so S must equal n*(A+B)/2 exactly. If input is consistent, yes. But could there be rounding issues? Since all are integers, we compute integer n.
Thus solution straightforward.
But we need to handle large numbers up to 10^16, so use 64-bit integers (long long in C++). Sum could be up to maybe 10^16, n could be up to? Possibly large but within 64-bit? Since S up to 10^16, n maybe up to 10^16? But n can't exceed because sum = n*(A+B)/2, with A and B also up to 10^16, so n could be as low as 7, but also maybe up to 210^16/(something). But A+B could be as small as maybe 2 (if A=1,B=1? but then AP would be constant? But A and B are third and third last terms, must be positive, minimum 1. So A+B at least 2. So n <= 210^16/2 = 10^16. So n could be up to 10^16, which is huge and printing the entire series would be impossible. However constraints: "All numbers will be less than 10^16." That likely refers to the given numbers (A, B, S) and maybe series elements? But they also say "The series will have at least 7 elements." However, if n can be as large as 10^16, printing the series would be impossible. So maybe n is limited. Usually in such problems, n is reasonably small. Or maybe the sum is such that n is within small range. But we need to read the problem statement carefully: "You need to print the length of the series & the series itself." That would be impractical if n is huge. So likely n is moderate. Also typical AP problems have n limited by constraints like sum fits in 64-bit, but n can be up to maybe 10^6? But printing up to 10^6 might be okay. But 10^16 is not feasible. So perhaps the actual input values are within smaller range, but the constraint "less than 10^16" is just for the numbers themselves, not for n. But n could still be large, but maybe the problem expects you to print the series, implying n is not huge. Could be that the test cases ensure n is manageable. Also the problem is from some contest where they might allow printing up to maybe 1000? Not sure.
But we can assume that n is manageable (like up to 10^5 or maybe 10^6) for output. However, we should not rely on that. But we can implement the solution as described, and if n is huge we cannot output all terms; but the problem statement likely expects output. So we assume that in test cases n is small enough.
But we should check: For given A, B, S, n = 2S/(A+B). Since S is up to 10^16 and A+B could be as low as 2, n could be up to 5e15. But is that possible while still having terms as positive integers? Let's examine typical constraints: They said "All numbers will be less than 10^16." That includes A, B, S. So S < 10^16, so S max approx 10^16 -1. A+B could be as low as maybe 2 (if A=1, B=1). That would give n up to about 210^16 /2 = 10^16, which is huge. But then the series would be constant? If A=1 and B=1 then from a+2d=1 and a+(n-3)d=1, we get (n-5)d=0 => d=0 (since n>=7, n-5>0). So d=0, then a=1. Sum = n 1 = n, and also S = n(A+B)/2 = n2/2 = n. So S=n, so n = S, which could be up to 10^16. So that would be a constant series of 1's of length 10^16. That would be impossible to print. But they require to print the series. So such test cases likely don't appear. Usually they restrict n to be reasonable. Many similar problems (SPOJ AP?) have n up to maybe 10^5. So we can safely assume that n is within output limits.
Given that, we can proceed.
Implementation steps:
For each test case:
- Read A, B, S as long long.
- Compute sum_ab = A + B.
- Compute n = (2S) / sum_ab. But need to check if 2S % sum_ab != 0 => maybe invalid? But per problem it's valid.
- Compute d_numer = B - A.
d_denom = n - 5.
Check if d_numer % d_denom != 0 => invalid? But should be valid.
- Compute d = d_numer / d_denom.
- Compute a = A - 2*d.
- Optionally check if a > 0 and a+(n-1)*d > 0. (But maybe not necessary if input valid.)
- Print n.
- Then print the series: for i=0..n-1, print a + i*d. Separate by spaces.
But note: The series may be huge if n large, but we'll assume small.
Potential pitfalls: integer overflow when computing 2S could exceed 64-bit if S up to 10^16? 210^16 = 2e16, which fits within signed 64-bit (max 9.22e18). So safe.
Also compute a and d; they may be negative? But if all terms positive, a > 0 and d could be negative but then last term positive; we should check.
But what about rounding? All divisions must be exact.
Now, confirm with example: Input: 3 8 55 => A=3, B=8, S=55. sum_ab=11, 2S=110, n=110/11=10. d_numer = 8-3=5, d_denom = 10-5=5 => d=1. a = 3-2*1=1. Series: 1..10. Sum = 55. Output matches.
Now, test another: Suppose A=10, B=7, S=34? Let's check: sum_ab=17, 2S=68 => n=4, but n>=7 required, so not valid. So probably not.
Now, what about decreasing AP? Example: a=10, d=-1, n=10 => series: 10,9,8,7,6,5,4,3,2,1. Then A = a+2d = 10-2=8? Actually 10,9,8 => third term=8. Third last term: 10 terms, third last = term index 8? Let's list: index1=10,2=9,3=8,4=7,5=6,6=5,7=4,8=3,9=2,10=1. Third last = 8th term? Actually "3rd last term" means the third term from the end, i.e., when counting from the last, the third. That would be term at position n-2? If first term is index 1, last is index n. The 3rd last term is the term at position n-2? Let's define: last term is n, second last is n-1, third last is n-2. So yes, third last term = a+(n-3)d. So for n=10, a=10,d=-1 => a+(n-3)d =10+7*(-1)=3. So B=3. A=8. S= sum=55. So input: 8 3 55 yields n=2S/(A+B)=110/11=10, d = (B-A)/(n-5) = (3-8)/5 = -5/5 = -1, a = A-2d = 8 -2*(-1)=8+2=10. So works. So we should handle negative d.
Thus code works.
Now, need to ensure that we print numbers as integers.
One more nuance: The problem statement says "Arithmetic and geometric Progressions are 2 of the well known progressions in maths." but only AP is used. Possibly they had a variant? But the task only mentions AP.
Thus solution seems straightforward.
Potential pitfalls: Could there be multiple solutions if A+B=0? Not possible because series elements positive => A,B positive => sum>0.
Another nuance: The sum formula S = n*(A+B)/2 holds always, regardless of d and a, as shown. So n is determined directly.
Now, we also need to verify that n is integer >=7. In the problem they guarantee valid input.
Now, one more point: Are we sure that the third term and third last term are exactly a+2d and a+(n-3)d? That's standard 1-indexing. They said "3rd term" and "3rd last term". Usually first term is a. So third term = a+2d. Third last term: last term = a+(n-1)d, second last = a+(n-2)d, third last = a+(n-3)d. So yes.
Thus solution is easy.
But we need to implement it.
Now, thinking about output: For each test case, output two lines: first line n, second line series separated by spaces. If many test cases, print accordingly.
Now, we should consider if there is any possibility of floating point or fraction? No, all integers.
Thus code:
For each test case:
read A, B, S (long long)
if (A+B == 0) // unlikely
handle? but not needed.
n = (2S)/(A+B);
if ( (2S) % (A+B) != 0 ) // but input ensures, we can still check and maybe error? but we assume correct.
d_numer = B - A;
d_denom = n - 5;
if (d_denom == 0) // not possible because n>=7 => n-5>=2
if (d_numer % d_denom != 0) // again assume.
d = d_numer / d_denom;
a = A - 2*d;
// check positivity of terms: a > 0 and a+(n-1)*d > 0 (optional)
// but perhaps a could be <=0? Then input invalid? But problem says series elements are positive integers, so it's guaranteed.
// print n and series.
// To avoid large output, we may limit but assume n manageable.
Implementation details in C++:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int T;
cin >> T;
while (T--) {
ll A, B, S;
cin >> A >> B >> S;
ll sum_ab = A + B;
ll n = (2 * S) / sum_ab; // integer division, must be exact
// It's safe if input consistent; we could assert remainder zero.
ll d = (B - A) / (n - 5); // as they are exact.
ll a = A - 2 * d;
cout << n << "\n";
for (ll i = 0; i < n; ++i) {
if (i > 0) cout << " ";
cout << a + i * d;
}
cout << "\n";
}
return 0;
}
But we need to be careful: integer division in C++ truncates towards zero. Since we know divisibility, it's fine.
Potential overflow: a + id might overflow if numbers large? but a and d are up to maybe 10^16? Actually A, B, S up to 10^16, n could be large, but a and d would be relatively small? Could be up to similar magnitude. But a + id for i up to n could be huge: for example, constant series with a=10^15 and n large, each term is 10^15, which is within bound. If d is large, say d=10^15, then a+(n-1)d could be huge, maybe > 10^16? But terms are less than 10^16? They said "All numbers will be less than 10^16." That might refer to input numbers, not necessarily series elements. But they also said "The series will have at least 7 elements. In all the test cases all the series elements are positive integers." No explicit bound on series elements besides being positive integers. Could be >10^16? Possibly, but since input numbers are less than 10^16, the series elements might be less than 10^16? Not necessarily; for example, a could be 1, d=100, n=1000, then terms up to 1+999*100=99901, still <10^16. Could be larger. But since n can be up to 10^16, terms could be huge. But again, they likely don't have huge n. So fine.
Now, we should consider if there is any tricky case: What if A+B is odd and 2S not divisible? But given problem constraints, it's consistent.
Thus solution is simple.
Now, I will produce the final answer. But need to be careful: The problem statement mentions "Arithmetic and geometric Progressions" but then only uses AP. Could there be geometric progression? The input: 3 numbers: 3rd term, 3rd last term, sum. For a GP, sum is different. But they didn't specify which progression it is. They might have intended to check both? Actually, read statement: "Arithmetic and geometric Progressions are 2 of the well known progressions in maths. Arithmetic progression (AP) is a set in which the difference between 2 numbers in constant. for eg, 1,3,5,7,9....In this series the difference between 2 numbers is 2.The task here is very simple indeed. You will be given the 3rd term , 3rd last term and the sum of the series. You need to print the length of the series & the series itself."
It says "Arithmetic and geometric Progressions are 2 of the well known progressions in maths." But then only defines AP. Possibly it's a mistake, or maybe they want to cover both? But the task says "You will be given the 3rd term , 3rd last term and the sum of the series." It doesn't specify which progression, but they've just explained AP. Usually such problems (maybe from CodeChef or SPOJ) are specifically about AP. I recall a problem named "AP" or "Arithmetic Progression" where you're given third term, third last term, sum. So it's definitely AP.
Thus solution is as above.
But before finalizing, I want to double-check the derived formula sum = n*(A+B)/2. That's because first+last = A+B. How? We derived first+last = 2a+(n-1)d. A+B = (a+2d)+(a+(n-3)d) = 2a+(n-1)d. Yes, so sum = n/2 * (first+last) = n/2*(A+B). So n = 2S/(A+B). That's correct.
Now, consider whether indexing could be 0-indexed? Usually, first term is index 1. So third term is index 3 => a+2d. Third last term: if there are n terms, the last is index n, third last is index n-2 => a+(n-3)d. Yes.
Thus solution.
Now, think about geometric progression: Could there be a scenario where it's a GP? The problem statement mentions both but only defines AP. Likely they just gave a general introduction, but the problem is about AP.
Thus final solution is straightforward.
But before delivering final answer, we should consider possible issues: The input numbers might be up to 10^16, we need to use long long (64-bit). In C++ we can use long long. In Python we can just use int (big int). I'll provide solution in Python because it's simpler and handles big integers. Also output format: after printing series, maybe need newline. The sample output: first line "10", second line "1 2 3 4 5 6 7 8 9 10". So no trailing spaces.
We'll implement in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
T = int(next(it))
out_lines = []
for _ in range(T):
A = int(next(it)); B = int(next(it)); S = int(next(it))
sum_ab = A + B
# compute n
n = (2 * S) // sum_ab # integer division
# compute d
d = (B - A) // (n - 5)
a = A - 2 * d
out_lines.append(str(n))
# generate series
series = ' '.join(str(a + i * d) for i in range(n))
out_lines.append(series)
sys.stdout.write('\n'.join(out_lines))
But be careful: n could be huge (like 10^16) and joining a huge list would be impossible. But we assume n is small. However, to be safe, we could check if n is very large and maybe output error? But the problem likely doesn't have such test cases.
Alternatively, we could derive that n is not that large because otherwise output would be too long. Typically judges accept outputs up to maybe 10^5 lines/characters. So we can trust.
Another nuance: The series elements could be large, but string conversion fine.
Now, test with given example.
Now, also test with decreasing example: A=8, B=3, S=55 -> n=10, d=-1, a=10, series 10 down to 1. Sum 55. Would that be accepted? Possibly, but problem says "all series elements are positive integers" (10..1 are positive). So it's valid.
Now, could there be a case where d is not integer? The problem ensures all series elements are integers, so d must be integer. However, we compute d as (B-A)/(n-5). Since we derived from integer equations, it should be integer. But we can assert divisible.
Now, what about rounding errors if using C++? Use long long.
Now, we can also compute using formula without dividing to avoid any fraction issues: we can compute n from sum, then compute d = (B-A)/(n-5) and a = A - 2*d. That's fine.
But need to be careful: In Python, integer division // yields floor for positive numbers, but if B-A negative, // also floor division? Actually Python's // is floor division, which for negative numbers gives floor (more negative). For example, -5 // 5 = -1, which is correct for exact division because -5/5 = -1 exactly, so it's fine. For exact division, it works.
Thus solution is correct.
Now, I'm going to produce final answer with explanation and code.
But wait, there is one more subtlety: The problem might also allow geometric progression? Could be that they give both possibilities and we need to determine which one? However, the description only defines AP, then says "the task here is very simple indeed. You will be given the 3rd term , 3rd last term and the sum of the series." It doesn't say "AP" explicitly in the task, but the description mentions AP. I think it's safe.
I recall a SPOJ problem called "AP2" where you are given 3rd term, 3rd last term, and sum. That's exactly this.
Thus final answer.
Now, I'll write the reasoning in the final solution.
Also include that we derive n = 2S/(A+B), d = (B-A)/(n-5), a = A - 2d.
Time complexity O(T + total length of series). If n is large, output size could be huge, but we assume it's within limits.
Edge Cases: n-5 might be zero if n=5, but n>=7, so not.
Now, final answer. We are given an arithmetic progression (AP) where the third term, third-last term, and the sum of all terms are provided.
Let the first term be (a) and common difference be (d). The number of terms is (n) ((n \ge 7)).
- Third term: (T_3 = a + 2d)
- Third‑last term: (T_{n-2} = a + (n-3)d)
- Sum: (S = \frac{n}{2}\bigl(2a + (n-1)d\bigr))
From the first two equations:
((a+(n-3)d) - (a+2d) = (n-5)d = (T_{n-2} - T_3)).
The sum can be expressed using the first and last term:
(S = \frac{n}{2}( \text{first} + \text{last} )).
But (\text{first} + \text{last} = (a+2d) + (a+(n-3)d) = T_3 + T_{n-2}).
Hence
[
S = \frac{n}{2}(T_3 + T_{n-2}) \quad\Longrightarrow\quad n = \frac{2S}{T_3 + T_{n-2}}.
]
Once (n) is known,
[
d = \frac{T_{n-2} - T_3}{n-5}, \qquad a = T_3 - 2d.
]
All divisions are exact because the input guarantees a valid AP with integer terms.
Steps for each test case:
- Read (T_3), (T_{n-2}), (S).
- Compute (n = 2S / (T_3 + T_{n-2})).
- Compute (d = (T_{n-2} - T_3) / (n - 5)).
- Compute (a = T_3 - 2d).
- Print (n) and then the series (a, a+d, \dots, a+(n-1)d).
The solution runs in (O(T + \text{total output length})) time, using 64‑bit integers (or Python’s arbitrary precision integers).
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
third = int(next(it))
third_last = int(next(it))
S = int(next(it))
# number of terms
n = (2 * S) // (third + third_last)
# common difference
d = (third_last - third) // (n - 5)
# first term
a = third - 2 * d
out.append(str(n))
out.append(' '.join(str(a + i * d) for i in range(n)))
sys.stdout.write('\n'.join(out))
if __name__ == "__main__":
solve()