位运算+贪心,这个解法是真的tql
AC代码:
#include<iostream>
using namespace std;
const int maxn=1e5+10;
int ans[maxn],cnt,sum,n;
int main() {
cin>>sum>>n;
for(int i=1; i<=n; i = i*2) {
for(int j=i; j<=n&∑ j+=i*2) {
if(sum&i || j+2*i<=n) { //***
ans[cnt++] = j;
sum -= i;
}
else break;
}
}
if(!sum){
cout<<cnt<<endl;
for(int i=0;i<cnt;i++) cout<<ans[i]<<" ";
}
else cout<<"-1"<<endl;
}