Make Good-cf

The meaning of problems : Given a sequence of length n, are calculated and their S , and XOR are P , you can add to the sequence number of three, so that the last S == 2 * P holds.

 

Thinking 1 : because a number of their own exclusive OR is 0, the first adding a P, then becomes  S + P and 0 , a plus S + P, since the XOR 0 = any number to any number, becomes 2 * (S + P) and S + P, to satisfy the meaning of problems.

 

Thought 2 : If S <2P , can be entered, plus two equal number (2P-S) / 2 , so that the subject condition is satisfied. The condition to be met is 1. 2P-S is even 2. S <2P. To satisfy the second condition, if S> 2P, the first entered, plus a large number, for example, a sequence 2 2, S = 4, P = 0, add into a w, satisfy the condition II. For a condition is, 2P has an even number, and to make an even number S, w is added to the list, the control line, if the original is an even number, an odd number added to the list, the original is odd, an even number added to the list, so the two conditions can be Satisfied.

 

Ideas 1 Code:

 

#include<iostream>
using namespace std;
int t,n;
int main()
{
    cin>>t;
    while(t--){
        cin>>n;
        int x;
        long long int sum=0,xr=0;
        for(int i=1;i<=n;i++){
            cin>>x;
            sum+=x;
            xr^=x;
        }
        if(sum==2*xr){
            cout<<0<<'\n'<<'\n';
            continue;
        }
        cout<<2<<'\n';
        cout<<xr<<" "<<xr+sum<<'\n';
    }
    return 0;
}
View Code

 

Ideas 2 code:

#include<iostream>
using namespace std;
const int maxn=1e5+10;
typedef long long ll;
int t,n;
ll a[maxn];
int main () {
    cin>>t;
    while(t--){
        cin>>n;
        ll s=0,p=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            s+=a[i];
            p^=a[i];
        }
        if(s==2*p){
            cout<<0<<endl;
            cout<<endl;
            continue;
        }
        ll w=(1ll<<50)+s%2;
        s+=w;p^=w;
        cout<<3<<endl;
        cout<<w<<" "<<(2*p-s)/2<<" "<<(2*p-s)/2<<endl;
    }
    return 0;
}
View Code

 

  

Guess you like

Origin www.cnblogs.com/qq2210446939/p/12123604.html