zoukankan      html  css  js  c++  java
  • Integer Sequence Dividing CodeForces

    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 (1n21091≤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

    Input
    3
    
    Output
    0
    
    Input
    5
    
    Output
    1
    
    Input
    6
    
    Output
    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), '', 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=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    ll n;
    int main()
    {
        cin>>n;
        ll ans=(n*(1+n))/2ll;
        if(ans&1)
        {
            cout<<1<<endl;
        }else
        {
            cout<<0<<endl;
        }
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }

    MY BLOG:
    https://www.cnblogs.com/qieqiemin/

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    Cobalt Strike使用的一些技巧
    中小团队选择一款合适的测试用例管理工具
    Angular 富文本编辑之路的探索
    一个研发团队是如何坚持7年技术分享的?
    Ubuntu远程连接显示错误Network error Connection refused解决方法
    一篇文章教会你使用HTML5加载音频和视频
    一篇文章带你了解CSS3按钮知识
    一篇文章带你了解HTML格式化元素
    一篇文章带你了解CSS3 3D 转换知识
    一篇文章带你了解SVG 蒙版(Mask)
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10254516.html
Copyright © 2011-2022 走看看