ZOJ 2531 Traveller (Gray码)

Traveller

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special Judge

A traveller plans a round trip through n cities, where n is a power of 2, in which case we simply index them with numeric values from 0 to n - 1. The traveller lives in one of them, and that's where he will start and end his trip. He only visits each city once, and for some particular reason the two adjacent cities on his trip should satify an equation that (A xor B) is also a power of 2.

For his odd mind no travel agency is willing to offer any help and finally he comes to you for a solution. You would either tell him it's not possible to arrange a trip for him.

Input

Input has two integers n and m, which are respectively the number of cities and the city he lives in.

Proceed through multiple cases until you meet a case n = 0.

Output

Print "NO" or a list of n city numbers on a single line separated by a single space.

Sample Input
2 1
4 0
0 0
Sample Output
1 0
0 1 3 2
#include <iostream>
using namespace std;

int DecimaltoGray ( int x )
{
    return x ^ ( x >> 1 );
}

int GraytoDecimal ( int x )
{
    int y = x;
    while ( x >>= 1 ) y ^= x;
    return y;
}

int main()
{
    int n, m;
    while ( 1 )
    {
        cin >> n >> m;
        if ( n == 0 ) break;
        for ( int i = 0; i < n - 1; i++ )
            cout << DecimaltoGray ( (i+m) % n ) << ' ';
        cout << DecimaltoGray ( (n-1+m) % n ) << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值