[构造]triples I

链接:https://ac.nowcoder.com/acm/contest/884/D?&headNav=acm&headNav=acm

来源:牛客网

题目描述

Doctor Elephant is testing his new program: output the bitwise or of the numbers inputed.
He has decided to input several multiples of 3 and the output of the program should be his favorite number  aaa.
Because he is lazy, he decided to input as few numbers as possible. He wants you to construct such an input for every  aaa he chose.
It's guaranteed that for every  aaa in the input there is such a solution. If there're multiple solutions you can output any.

输入描述:

There're multiple test cases in a test file.
The first line contains a positive integer  T - the number of test cases.
In each of the following  T lines there is one positive integer aaa.

输出描述:

For each test case output a line. First you need to output the number of numbers in your input, and then you need to output these numbers, separating by spaces.
示例1

输入

复制
2
3
7

输出

复制
1 3
2 3 6

说明

3=3, (3|6)=7

备注:

1≤T≤1051 \leq T \leq 10^51T105, 1≤a≤10181 \leq a \leq 10^{18}1a1018.

思路:

AC代码:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

vector<ll> v[5];

void init(ll a){
  ll now=1;
  while(a){
    if(a&1) v[now%3].push_back(now);
    now<<=1;
    a>>=1;
  }
}

int main()
{
    ll t;scanf("%lld",&t);
    while(t--){
    ll a;scanf("%lld",&a);
    for(ll i=0;i<3;i++) v[i].clear();
    if(a%3==0) printf("1 %lld\n",a);
    else{
        init(a);

        printf("2 ");
        if(a%3==1){
            if(v[1].size()>=2){
                ll p=v[1][0],q=v[1][1];
                printf("%lld %lld\n",a-p,a-q);
            }
            else if(v[1].size()==1){
                ll p=v[1][0],q=v[2][0];
                printf("%lld %lld\n",a-p,p+q);
            }
            else{
                ll p=v[2][0],q=v[2][1],r=v[2][2];
                printf("%lld %lld\n",a-p-q,p+q+r);
            }
        }else{
            if(v[2].size()>=2){
                ll p=v[2][0],q=v[2][1];
                printf("%lld %lld\n",a-p,a-q);
            }
            else if(v[2].size()==1){
                ll p=v[2][0],q=v[1][0];
                printf("%lld %lld\n",a-p,p+q);
            }
            else{
                ll p=v[1][0],q=v[1][1],r=v[1][2];
                printf("%lld %lld\n",a-p-q,p+q+r);
            }
        }
    }
    }
    return 0;
}
View Code
 

猜你喜欢

转载自www.cnblogs.com/lllxq/p/11256345.html
I