A. A.M. Deviation
A number a2 is said to be the arithmetic mean of two numbers a1 and a3, if the following condition holds: a1+a3=2⋅a2.
We define an arithmetic mean deviation of three numbers a1, a2 and a3 as follows: d(a1,a2,a3)=|a1+a3−2⋅a2|.
Arithmetic means a lot to Jeevan. He has three numbers a1, a2 and a3 and he wants to minimize the arithmetic mean deviation d(a1,a2,a3). To do so, he can perform the following operation any number of times (possibly zero):
- Choose i,j from {1,2,3} such that i≠j and increment ai by 1 and decrement aj by 1
Help Jeevan find out the minimum value of d(a1,a2,a3) that can be obtained after applying the operation any number of times.
The first line contains a single integer t (1≤t≤5000) — the number of test cases.
The first and only line of each test case contains three integers a1, a2 and a3 (1≤a1,a2,a3≤108).
For each test case, output the minimum value of d(a1,a2,a3) that can be obtained after applying the operation any number of times.
3
3 4 5
2 2 6
1 6 5
0
1
0
Note that after applying a few operations, the values of a1a1, a2a2 and a3a3 may become negative.
思路:从题意你可以发现,只涉及到a和c的改变是不会改变最后的值的,只有涉及到a和b以及c和b的时候才会影响最后的值。在观察一下,如果a+1,b-1,那么最后的值就会+3,同理如果a-1,b+1,那么最后的值就-3。那其实到这我们就发现了一个规律,变化的量是3。那就好办了,既然这样,那我就把最后的结果求余%3,如果等于零,那就最小是零,如果不等于0,那最小就是1。(永远不是2的原因也很简单,如果结果加个3或者减个3,最小值不就变成1了嘛)
题解:
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int n;
int main(){
cin>>n;
while(n--){
int a,b,c;
cin>>a>>b>>c;
cout<<(abs(a+c-2*b)%3 ? 1:0)<<endl;
//如果增加减少c和a不改变原来的结果
//如果改变了a或c其中一个,就会改变最终结果+3或者-3
//也就是说,如果能被3整除就说明最小值是0,否则会变成1或者2
//如果算绝对值的话最小只能是1 !!
}
return 0;
}

浙公网安备 33010602011771号