Codeforces Round #554 (Div. 2) Problem B. Neko Performs Cat Furrier Transform

26 篇文章 0 订阅
13 篇文章 0 订阅

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值