A - 369 Editorial by en_translator
If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).
For simplicity, we assume \(A \leq B\). (Otherwise, we may swap \(A\) and \(B\).)
First, one can construct an arithmetic sequence by arranging \(A,B,x\) in some order if and only if one can construct an arithmetic sequence by arranging \(A,B,x\) in ascending order. This is because, every sequence is sorted in ascending or descending order, and for the latter case, reversing it yields an arithmetic sequence sorted in ascending order. Thus, the condition regarding \(x\) can be represented as follows:
- Condition: Among \(A,B\), and \(x\), the difference between the first and second largest values is equal to the difference between the second and third largest values.
We consider two cases: \(A=B\) and \(A<B\).
If \(A=B\)
The condition is satisfied if and only if \(x=A=B\). Thus, the answer is \(1\).
If \(A<B\)
There are three patterns for the order of \(A,B\), and \(x\): \(x<A<B\), \(A<B<x\), and \(A<x<B\). (We rule out the possibility that \(x\) equals either \(A\) or \(B\), in which case the condition is never satisfied.)
If \(x < A < B\), the condition is represented as \(B-A = A-x\), so the sought value \(x\) is uniquely determined as \(x=2A-B\).
Likewise, if \(A < B < x\), the conforming \(x\) is uniquely determined as \(x=2B-A\).
If \(A < x < B\), the condition is represented as \(B-x = x-A\). Solving it yields \(x = \frac{A+B}{2}\), but \(x\) must be an integer, so a conforming \(x\) exists if and only if \(A+B\) is even.
Summary
By the discussion above, the answer to this problem is
- \(1\) if \(A=B\);
- \(3\) if \(A\neq B\) and \(A+B\) is even;
- \(2\) if \(A\neq B\) and \(A+B\) is odd.
All that left is to implement this conditional branches with if
statements.
One can determine if an integer is even or not by checking if the remainder when divided by \(2\) is \(0\). (Most programming languages have an operator that finds a remainder.)
For more details on implementation, please refer to the sample code (C++ and Python).
Sample code (C++) :
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
if (a == b) {
cout << 1 << endl;
} else if ((a + b) % 2 == 0) {
cout << 3 << endl;
} else {
cout << 2 << endl;
}
}
Sample code (Python) :
a, b = map(int, input().split())
if a == b:
print(1)
elif (a + b) % 2 == 0:
print(3)
else:
print(2)
posted:
last update: