https://codeforces.com/contest/1152/problem/B
大致思路是找到二进制n的第一个0, 然后模拟,与位操作有关
#include<algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define SZ(x) ((int)x.size())
#define rep(i,a,n) for (int i = a; i < n; ++i)
#define per(i,a,n) for (int i = n-1; i >= a; --i)
#define devil_may_cry ios::sync_with_stdio(false);
using namespace std;
//head
int a[64];
int a_num;
void Init(int num)
{
a_num = 0;
while (num)
{
if (num & 1)
a[a_num++] = 1;
else
a[a_num++] = 0;
num >>= 1;
}
rep(i, 0, a_num / 2)
{
int j = a[i];
a[i] = a[a_num - i - 1];
a[a_num - i - 1] = j;
}
}
bool isP(int num)
{
bool flag = true;
num++;
while (num)
{
if (num % 2 != 0 && num != 1)
flag = false;
num = num >> 1;
}
return flag;
}
int main()
{
int num, _ = 40;
int NUM = 0;
int ans[44];
int ans1 = -1;
int ans_num = 0;
cin >> num;
if (isP(num))
{
return !printf("0\n");
}
ans_num = 0;
rep(q, 1, _ + 1)
{
ans[ans_num] = 1;
int k = -1;
if (q & 1)
{
Init(num);
rep(i, 1, a_num)
{
if (a[i] == 0)
{
k = a_num - i + 1;
break;
}
}
if (k == -1)
{
ans1 = q - 1;
goto g1;
}
k--;
while (k--)
{
ans[ans_num] = ans[ans_num] << 1;
}
ans[ans_num]--;
num = num ^ ans[ans_num];
if (isP(num))
{
ans_num++;
ans1 = q;
goto g1;
}
ans_num++;
}
else
{
num++;
if (isP(num))
{
ans1 = q;// -1;
break;
}
}
}
g1:
cout << ans1 << endl;
rep(i, 0, ans_num)
{
int j = 1;
while (ans[i] != 1)
{
ans[i] >>= 1;
j++;
}
cout << j << " ";
}
cout << endl;
//system("pause");
return 0;
}