首先有一个引理
引理1
对于 和 (),我们只会选 或 其中之一
证明
显然,我们如果选择 和 ,那么我们可以构造出 ,并且还有机会构造出更多的数,所以一定不劣,选择 的情况同理
我们考虑暴力怎么做,一个显然的思路是我们枚举 位是选 还是 还是不选,我们发现如果我们选择不选,那么我们在之前的处理中所得到的新序列 中就不应该存在一个 使得 的 位为 ,因为如果这样那么我们就必须得通过 (进位)或者 (消掉这一位)来保证答案的正确性。这份暴力的复杂度为
接下来我们引入一个优化来让复杂度变正确
我们在刚开始把 序列排序,然后每次操作前对 序列全部右移一位并去重,即可把复杂度降为
复杂度证明
先证正确性
正确性证明
右移
我们在做完 或者 的操作后,序列中每个数的 位上都应为 ,故其不会对之后的搜索产生影响,故我们可以删去最后一位,这样也便于我们的处理(只需要处理最低位)
去重
这部分也很好理解,因为如果我们已经可以表示出 了,那么序列中所有剩下的所有等于 的(包括进行右移后等于的)都可以直接删去,因为他们和 会一并被求出
然后是复杂度的结果的证明
复杂度证明
我们令原序列为最坏情况,即全部 种全部出现,那么对于一次右移前,一定会存在 (即两数仅最低位不同)的情况,那么在右移和去重后这两个数就会变成一个数,也就是 ,也就是每次序列长度至少减少一半,即最多进行 轮,每轮的复杂度为 ,那么复杂度就是
代码:
#include <bits/stdc++.h>
using namespace std;
namespace MySpace{
#define lowbit(x) (x&(-x))
#define sqrt sqrtl
using pii=pair<int,int>;
#define whileu(p) while (p--)
#define forl(a,b,c) for (auto a=b;a<=c;a=a+1)
inline void read() {}
template <typename T, typename... R>
inline void read(T &x,R &... oth){
x=0;T f=1;
char c=getchar();
while(c<'0' || c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0' && c<='9') x=(x<<1)+(x<<3)+(c&15),c=getchar();
x*=f;
read(oth...);
return;
}
template<typename T>
T qpow(T a,T n,T p){
T res=1;a%=p;
while (n){
if (n&1) res=1ll*res*a%p;
a=1ll*a*a%p;
n>>=1;
}
return res;
}
template<typename T>
T gcd(T a,T b){return (b>0?gcd(b,a%b):a);}
template<typename T>
T exgcd(T a,T b,T& x,T& y){
if (b<=0ll){
x=1ll,y=0ll;
return a;
}
T d=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return d;
}
template<typename T>
T inv(T a,T p){
if (a==1) return 1ll;
T x,y;
if (exgcd(a,p,x,y)!=1ll) return -1ll;
else return (x+p)%p;
}
}
using namespace MySpace;
//const int INF=0x66CCFF66;
#ifndef ONLINE_JUDGE
#include "G:\CodeFloder\debug.h"
using namespace DEBUGs;
#endif
#define int long long
using vi=vector<int>;
const int maxn=1e5+5;
int n;
vi a,ans;
void does(vi& a){
//sort(a.begin(),a.end());
for (auto &i:a) i>>=1;
a.erase(unique(a.begin(),a.end()),a.end());
return;
}
vi dfs(int x){
if (x>19) return vi(0);
for (auto i:a) if (i&1){
vi tmp=a;
for (auto &i:a) if (i&1) i+=1;
does(a);
vi ta=dfs(x+1);
a=tmp;
for (auto &i:a) if (i&1) i-=1;
does(a);
vi tb=dfs(x+1);
ta.push_back(-(1ll<<x));
tb.push_back((1ll<<x));
return ta.size()<tb.size()?ta:tb;
}
does(a);
return dfs(x+1);
}
signed main(){
read(n);
for (int i=1;i<=n;i++) {
int tmp;read(tmp);
a.push_back(tmp);
}
sort(a.begin(),a.end());
ans=dfs(0);
printf("%d\n",ans.size());
for (auto i:ans) printf("%d ",i);
return 0;
}