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 Input2 1 4 0 0 0Sample 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; }
一个旅行者计划通过n个城市进行往返旅行,其中n为2的幂次方。每个城市只访问一次,且相邻城市的访问需满足特定的数学条件。通过输入城市数量n和居住城市编号m,该算法能生成符合要求的旅行路线或确认不可实现。
887

被折叠的 条评论
为什么被折叠?



