-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAP2.cs
43 lines (37 loc) · 1.47 KB
/
AP2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
// https://www.spoj.com/problems/AP2/ #math #sequence
// Given certain terms and the sum of an arithmetic progression, finds the whole series.
public static class AP2
{
public static long[] Solve(long thirdTerm, long thirdToLastTerm, long sum)
{
// In an AP, note that pairs like (a_1, a_n), (a_2, a_n-1) always add up to the same thing.
// A little more work shows (a_b + a_n-b) * (n/2) = sum, so n = 2 * sum / (a_b + a_n-b).
int n = (int)(2 * sum / (thirdTerm + thirdToLastTerm));
// Now we know the third to last term's index is n - 2, and the third term's index is 3.
int progressionsBetweenGivenTerms = (n - 2) - 3;
long termDifference = (thirdToLastTerm - thirdTerm) / progressionsBetweenGivenTerms;
long firstTerm = thirdTerm - 2 * termDifference;
long[] series = new long[n];
series[0] = firstTerm;
for (int i = 1; i < n; ++i)
{
series[i] = series[i - 1] + termDifference;
}
return series;
}
}
public static class Program
{
private static void Main()
{
int remainingTestCases = int.Parse(Console.ReadLine());
while (remainingTestCases-- > 0)
{
long[] line = Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
long[] series = AP2.Solve(line[0], line[1], line[2]);
Console.WriteLine(series.Length);
Console.WriteLine(string.Join(" ", series));
}
}
}