People's vaccine
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk loves drinking tea very much, because it is a good prevention against burdockvirus.
Zenyk takes tea drinking very responsibly, and therefore always follows strict rules: he drinks tea every hour, starting at 9 AM and ending at 9 PM, the volume of tea drunk is always an integer. In addition, Zenyk firmly believes that for a better effect, all differences between the volumes of tea drunk during two consecutive tea drinking sessions must be the same throughout the day. That is, the volumes of tea drunk during the day must form an arithmetic progression.
Every morning Zenyk wakes up and plans his tea drinking for that day. To do this, he chooses two integers \(a\) — the volume of tea to drink at 9 AM, and \(b\) — the volume of tea to drink at 9 PM.
Next, Zenyk calculates how much tea in total he will drink during the day if he follows all his rules? This daily procedure exhausts Zenyk.
Can you write a program to help him with the calculations?
Input
The single line contains two integers \(a\) and \(b\) — the volumes of tea to be drunk at 9 AM and 9 PM, respectively.
Output
If an integer sequence satisfying all the requirements does not
exist, output -1. Otherwise, output an integer — the total
volume of tea drunk during the day.
Constraints
\(1 \le a, b \le 10^9\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 1 13 | 91 |
| Input (stdin) | Output (stdout) |
|---|---|
| 2 17 | -1 |
Notes
\(1+2+3+4+5+6+7+8+9+10+11+12+13=91\).
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
People's vaccine - Editorial
We are given the first and the thirteenth terms of an arithmetic progression. By definition, \(A_k = A_1 + (k-1) \cdot d\), where \(A_i\) is the \(i\)-th term of the progression and \(d\) is its common difference. It follows that \(d = \frac{A_k - A_1}{k-1}\), which for \(k=13\) gives \(d = \frac{A_{13} - A_1}{12}\).
Clearly, all terms of an arithmetic progression are integers only if its first term and its difference are integers. The statement guarantees that the first term is an integer, so we only need to check the difference, that is, whether \(B-A\) is divisible by 12.
If the difference is an integer (note that it can be negative, which does not matter), we need to find the sum of the first thirteen terms of the progression. The well-known formula for the sum of the first \(k\) terms is \(S_k = \frac{(A_1 + A_k) \cdot k}{2}\), so the answer is \(\frac{(A+B) \cdot 13}{2}\).
Complexity: \(O(1)\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|