Integer Sequence Dividing 寒假集训思维题 找规律

1. Integer Sequence Dividing 寒假集训思维题 找规律


##链接CodeForces - 1102A
##题目:

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;
 } 
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值