Integer Sequence Dividing CodeForces - 1102A (规律)
You are given an integer sequence 1,2,…,n1,2,…,n. You have to divide it into two sets AAand BB in such a way that each element belongs to exactly one set and |sum(A)−sum(B)||sum(A)−sum(B)| is minimum possible.
The value |x||x| is the absolute value of xx and sum(S)sum(S) is the sum of elements of the set SS.
Input
The first line of the input contains one integer nn (1≤n≤2⋅1091≤n≤2⋅109).
Output
Print one integer — the minimum possible value of |sum(A)−sum(B)||sum(A)−sum(B)| if you divide the initial sequence 1,2,…,n1,2,…,n into two sets AA and BB.
Examples
3
0
5
1
6
1
Note
Some (not all) possible answers to examples:
In the first example you can divide the initial sequence into sets A={1,2}A={1,2} and B={3}B={3} so the answer is 00.
In the second example you can divide the initial sequence into sets A={1,3,4}A={1,3,4} and B={2,5}B={2,5} so the answer is 11.
In the third example you can divide the initial sequence into sets A={1,4,5}A={1,4,5} and B={2,3,6}B={2,3,6} so the answer is 11.
题意:给你一个整数N,让你将1~N这N个整数分成两个集合,
问这两个集合的元素数值和的差最小能是多少。
思路:
先写几个样例来看下。
当N=3,
1,2,3 可以把1和2分到一个集合,3分到另一个集合。这样差为0
当N=4
1,2,3,4可以把 1和4分到一个集合,2和3在另一个集合,这样差为0
当N=5
1,2,3,4,5,可以分成这样{1,3,4},{2,5} 差为1
我们在算下这三个样例的所有元素和
N=3 ,sum=6
N=4,sum=10
N=5,sum=15
规律就可以看出来了,当1~N的和为偶数的时候,一定可以分成两个相同的sum的集合
为奇数可以分成相差为1的两个集合。
那么就根据规律来写程序了。
我的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n;
int main()
{
cin>>n;
ll ans=(n*(+n))/2ll;
if(ans&)
{
cout<<<<endl;
}else
{
cout<<<<endl;
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
MY BLOG:
https://www.cnblogs.com/qieqiemin/
Integer Sequence Dividing CodeForces - 1102A (规律)的更多相关文章
- CodeForces - 1102A
You are given an integer sequence 1,2,-,n1,2,-,n. You have to divide it into two sets AA and BB in s ...
- CodeForces - 1102A(思维题)
https://vjudge.net/problem/2135388/origin Describe You are given an integer sequence 1,2,-,n. You ha ...
- Codeforces Round #452 (Div. 2)-899A.Splitting in Teams 899B.Months and Years 899C.Dividing the numbers(规律题)
A. Splitting in Teams time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 递推:Number Sequence(mod找规律)
解题心得: 1.对于数据很大,很可怕,不可能用常规手段算出最后的值在进行mod的时候,可以思考找规律. 2.找规律时不必用手算(我傻,用手算了好久).直接先找前100项进行mod打一个表出来,直接看就 ...
- CodeForces - 810C(规律)
C. Do you want a date? time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
- Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)
链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...
- CodeForces - 1059C Sequence Transformation (GCD相关)
Let's call the following process a transformation of a sequence of length nn. If the sequence is emp ...
- Sequence in the Pocket【思维+规律】
Sequence in the Pocket 题目链接(点击) DreamGrid has just found an integer sequence in his right pocket. A ...
随机推荐
- Word中类似正则匹配的查找替换通配符的使用详解
一.Word查找栏代码&通配符一览表 序号 清除使用通配符复选框 勾选使用通配符复选框 特殊字符 代码 特殊字符 代码or通配符 1 任意单个字符 ^? 任意单个字符 ? 2 任意数字 ^# ...
- webpack 开发环境与生成环境的 配置
写在前面 最近学习react,之前做vue项目的时候,一直都是拿来主义,浑浑噩噩,感觉不太好,趁学习react的机会,在顺带学习一下webpack.一般配置文件分两份,为开发环境和生成环境.有此区分, ...
- HTML5原生拖拽事件的值传递(三dataTransfer对象)
引用一篇博客,讲解的比较详细:http://www.tuicool.com/articles/j6Zbam
- 5分钟入门Tornado
Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了能有效 ...
- lij IDEA项目包分层结构显示设置
使用Intellij IDEA创建项目发现包没有分层,使用不方便. 可以点击右上角的设置,把红框选项的√去掉即可. 就会分层显示了,这就很舒服了.
- 为什么签名前要加"\x19Ethereum Signed Message:\n"
在以太坊中,可以找到关于上述破损的解释例子.以太坊有两种消息,交易
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- Linux系统学习之正则表达式
一.基础的正则表达式 1."."(一个点)符号 点符号用于U匹配除换行符号之外的任意一个字符.例如:r.t可以匹配rot.rut,但是不能匹配root,但如果使用r..t,就可以匹 ...
- 深入浅出的webpack4构建工具--webpack4+vue+vuex+mock模拟后台数据(十九)
mock的官网文档 mock官网 关于mockjs的优点,官网这样描述它:1)可以前后端分离.2)增加单元测试的真实性(通过随机数据,模拟各种场景).3)开发无侵入(不需要修改既有代码,就可以拦截 A ...
- scp传输文件的命令
scp传输文件的命令 一:scp常规的使用方式: scp可以进行简单的远程复制文件的功能.它是一个在各个主机之间进行复制或文件传输的一个命令工具.它使用一种同ssh一样的安全机制来进行文件的传输. 注 ...