Codeforces Round #554 (Div. 2)B. Neko Performs Cat Furrier Transform【模拟】

猫程序员的完美长猫算法
探讨了CatFurrierTransform算法,一种流行于猫程序员社区的算法,用于创造理想的长猫形态。该算法通过一系列特定操作转换数字,目标是将任意给定的数字转化为形式为2^m-1的数,以符合完美长猫的标准。本文详细介绍了算法的步骤,包括位运算和加法操作,并提供了一个实现示例。

Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.

Assume that we have a cat with a number xx. A perfect longcat is a cat with a number equal 2m−12m−1 for some non-negative integer mm. For example, the numbers 00, 11, 33, 77, 1515 and so on are suitable for the perfect longcats.

In the Cat Furrier Transform, the following operations can be performed on xx:

  • (Operation A): you select any non-negative integer nn and replace xx with x⊕(2n−1)x⊕(2n−1), with ⊕⊕ being a bitwise XOR operator.
  • (Operation B): replace xx with x+1x+1.

The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.

Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 4040 operations. Can you help Neko writing a transformation plan?

Note that it is not required to minimize the number of operations. You just need to use no more than 4040 operations.

Input

The only line contains a single integer xx (1≤x≤1061≤x≤106).

Output

The first line should contain a single integer tt (0≤t≤400≤t≤40) — the number of operations to apply.

Then for each odd-numbered operation print the corresponding number nini in it. That is, print ⌈t2⌉⌈t2⌉ integers nini (0≤ni≤300≤ni≤30), denoting the replacement xx with x⊕(2ni−1)x⊕(2ni−1) in the corresponding step.

If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.

思路:这题保证有答案,所以我们就模拟来写,第一步是异或,第二步是加一,一直到有解为止。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 10;
int a[100], ans[50];
int cnt;
void slove(int n)
{
    while(n)
    {
        a[++cnt] = n % 2;
        n /= 2;
    }
}
int Pow(int x)
{
    int res = 1;
    for(int i = 1; i <= x; ++i)
        res = res * 2;
    return res;
}

int main()
{
    int n;
    scanf("%d", &n);
    bool flag = true;
    int num = 0, t = 0;
    while(flag)
    {
        cnt = 0;
        slove(n);
        int pos = 0;
        for(int i = cnt; i >= 0; --i)
        {
            if(i == 0)
            {
                flag = false;
                break;
            }
            if(a[i] == 0)
            {
                pos = i;
                break;
            }
        }
        if(num % 2 == 0)
        {
            if(flag)
            {
                n = n ^ (Pow(pos) - 1);
                ans[t++] = pos;
            }
        }
        else
        {
            n = n + 1;
        }
        if(flag)
            ++num;
    }
    printf("%d\n", num);
    if(num > 0)
    {
        for(int i = 0; i < t - 1; ++i)
            printf("%d ", ans[i]);
        printf("%d\n", ans[t - 1]);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值