zoukankan      html  css  js  c++  java
  • Good Bye 2019 C. Make Good

    链接:

    https://codeforces.com/contest/1270/problem/C

    题意:

    Let's call an array a1,a2,…,am of nonnegative integer numbers good if a1+a2+⋯+am=2⋅(a1⊕a2⊕⋯⊕am), where ⊕ denotes the bitwise XOR operation.

    For example, array [1,2,3,6] is good, as 1+2+3+6=12=2⋅6=2⋅(1⊕2⊕3⊕6). At the same time, array [1,2,1,3] isn't good, as 1+2+1+3=7≠2⋅1=2⋅(1⊕2⊕1⊕3).

    You are given an array of length n: a1,a2,…,an. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.

    思路:

    想多了发现是sb题,让右边为0,加个左边就行了

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int MAXN = 1e5+10;
    
    int n;
    int a[MAXN];
    
    int main()
    {
        int t;
        cin >> t;
        while(t--)
        {
            cin >> n;
            LL ans = 0, sum = 0;
            for (int i = 1;i <= n;i++)
            {
                cin >> a[i];
                ans ^= a[i];
                sum += a[i];
            }
            if (ans*2 == sum)
                cout << 0 << endl << endl;
            else if (ans == 0)
            {
                cout << 1 << endl;
                cout << sum << endl;
            }
            else
            {
                sum += ans;
                cout << 2 << endl;
                cout << ans << ' ' << sum << endl;
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    java集合--使用集合应该关注的方面
    Linux环境Java多版本管理与切换
    java集合--LinkedList源码
    Java集合--ArrayList源码
    数据库规范化(范式)
    Java异常
    Java内部类
    Java抽象类和接口
    JavaScript 语句
    Vue.js简介
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12122445.html
Copyright © 2011-2022 走看看