B. Make AP
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp has 3 positive integers 𝑎, 𝑏 and 𝑐. He can perform the following operation exactly once.
Choose a positive integer 𝑚 and multiply exactly one of the integers 𝑎, 𝑏 or 𝑐 by 𝑚.
Can Polycarp make it so that after performing the operation, the sequence of three numbers 𝑎, 𝑏, 𝑐 (in this order) forms an arithmetic progression? Note that you cannot change the order of 𝑎, 𝑏 and 𝑐.
Formally, a sequence 𝑥1,𝑥2,…,𝑥𝑛 is called an arithmetic progression (AP) if there exists a number 𝑑 (called “common difference”) such that 𝑥𝑖+1=𝑥𝑖+𝑑 for all 𝑖 from 1 to 𝑛−1. In this problem, 𝑛=3.
For example, the following sequences are AP: [5,10,15], [3,2,1], [1,1,1], and [13,10,7]. The following sequences are not AP: [1,2,4], [0,1,0] and [1,3,2].
You need to answer 𝑡 independent test cases.
Input
The first line contains the number 𝑡 (1≤𝑡≤104) — the number of test cases.
Each of the following 𝑡 lines contains 3 integers 𝑎, 𝑏, 𝑐 (1≤𝑎,𝑏,𝑐≤108).
Output
For each test case print “YES” (without quotes) if Polycarp can choose a positive integer 𝑚 and multiply exactly one of the integers 𝑎, 𝑏 or 𝑐 by 𝑚 to make [𝑎,𝑏,𝑐] be an arithmetic progression. Print “NO” (without quotes) otherwise.
You can print YES and NO in any (upper or lower) case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer).
Example
inputCopy
11
10 5 30
30 5 10
1 2 3
1 6 3
2 6 3
1 1 1
1 1 2
1 1 3
1 100000000 1
2 1 1
1 2 2
outputCopy
YES
YES
YES
YES
NO
YES
NO
YES
YES
NO
YES
Note
In the first and second test cases, you can choose the number 𝑚=4 and multiply the second number (𝑏=5) by 4.
In the first test case the resulting sequence will be [10,20,30]. This is an AP with a difference 𝑑=10.
In the second test case the resulting sequence will be [30,20,10]. This is an AP with a difference 𝑑=−10.
In the third test case, you can choose 𝑚=1 and multiply any number by 1. The resulting sequence will be [1,2,3]. This is an AP with a difference 𝑑=1.
In the fourth test case, you can choose 𝑚=9 and multiply the first number (𝑎=1) by 9. The resulting sequence will be [9,6,3]. This is an AP with a difference 𝑑=−3.
In the fifth test case, it is impossible to make an AP.
题意 :
- 给三个正整数,问是否能选择其中一个乘上一个正整数,使它们三个按原先顺序成等差数列
思路 :
- 三种情况分类讨论
- 注意
b % a == 0
不一定是b是a的倍数,还有b为0的可能性,而这里,前者不可以是0(因为乘上正整数)
#include <iostream>
#define endl '\n'
using namespace std;
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int _; cin >> _;
while (_ -- )
{
int a, b, c; cin >> a >> b >> c;
auto check = [&]()
{
if ((2 * b - c) > 0 && (2 * b - c) % a == 0) return 1;
else if ((a + c) % (2 * b) == 0) return 1;
else if ((2 * b - a) > 0 && (2 * b - a) % c == 0) return 1;
else return 0;
};
if (check()) cout << "yes" << endl;
else cout << "no" << endl;
}
}