A. Integer Sequence Dividing
You are given an integer sequence 1,2,…,nYou have to divide it into two sets A and B in such a way that each element belongs to exactly one set and |sum(A)−sum(B)| is minimum possible.
The value |x|is the absolute value of x and sum(S) is the sum of elements of the set S.
Input
The first line of the input contains one integer n (1≤n≤2⋅109).
Output
Print one integer — the minimum possible value of |sum(A)−sum(B)| if you divide the initial sequence 1,2,…,n into two sets A and B.
Examples
inputCopy
3
outputCopy
0
inputCopy
5
outputCopy
1
inputCopy
6
outputCopy
1
Note
Some (not all) possible answers to examples:
In the first example you can divide the initial sequence into sets A={1,2} and B={3} so the answer is 0.
In the second example you can divide the initial sequence into sets A={1,3,4}and B={2,5} so the answer is 11.
In the third example you can divide the initial sequence into sets A={1,4,5} and B={2,3,6} so the answer is 11.
##题目大意:给一个数n,把1,2,3,4…n随机分成两个集合sum(A)和sum(B),求| sum(A)- sum(B)|的最小值。
##题目分析:
这道题我们可以联想到高斯算法
例如
n = 4
我们发现 1+4 = 2 + 3这时我们就可以把1,2,3,4分成两个数集。
但这里有一个问题,偶数和奇数的问题,因为n为偶数的话正好可以分成k个元素为二的小集合,但如果n为奇数的话不能。
接下来分别考虑偶数和奇数
##偶数
偶数的话需要考虑一个问题,上面我们n = 4时正好可以分成(偶数)两个元素为2的小集合,然后我们可以把这两个集合平均分sun(A)和sun(B)中,这样最小值为0。同理,如果n = k,我们可以分成偶数个元素为二的集合那么最小值就是0。(自己想想为什么)
那么如果分成奇数个元素为二的集合如n = 6时分成三个小集合(1,6)(2,5)(3,4)那么最小值就是1(自己想想为什么)
##奇数
奇数的话就是找规律
可以发现当n/2%2==0 时 min = 1
当 n/ 2 %2 == 1时 min = 0;(想一想这是为什么,这很重要)
##下面是代码
#include<stdio.h>
int main()
{
__int64 a;
scanf("%I64d",&a);
if(a % 2 == 0)
{
if(a / 2 % 2 == 0)
printf("0\n");
else if (a / 2 % 2 == 1)
printf("1\n");
}
else
{
if(a / 2 % 2 == 0)
printf("1");
else if(a / 2 % 2 == 1)
printf("1");
}
return 0;
}