链接: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≤T≤1051 \leq T \leq 10^51≤T≤105, 1≤a≤10181 \leq a \leq 10^{18}1≤a≤1018.
思路:
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; }
本文针对一个具体的编程问题,即通过位运算求解特定输入条件下输出的最小数量的多个3的倍数,使得这些数的按位或结果等于给定的目标数。文章详细介绍了算法思路,包括如何使用位运算和模运算来确定所需输入数的数量和具体数值,并提供了一段完整的C++代码实现。
661

被折叠的 条评论
为什么被折叠?



