Piegirl found the red button. You have one last chance to change the inevitable end.

The circuit under the button consists of n nodes, numbered from 0 to n - 1. In order to deactivate the button, the n nodes must be disarmed in a particular order. Node 0 must be disarmed first. After disarming node i, the next node to be disarmed must be either node (2·i) modulo n or node (2·i) + 1 modulo n. The last node to be disarmed must be node 0. Node 0 must be disarmed twice, but all other nodes must be disarmed exactly once.

Your task is to find any such order and print it. If there is no such order, print -1.

Input

Input consists of a single integer n (2 ≤ n ≤ 105).

Output

Print an order in which you can to disarm all nodes. If it is impossible, print -1 instead. If there are multiple orders, print any one of them.

Examples

Input
2
Output
0 1 0
Input
3
Output
-1
Input
4
Output
0 1 3 2 0
Input
16
Output
0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0

题意:给定点[0,N-1],点i到i*2%N,i*2+1%N连边,问是否存在从0出发的哈密尔顿回路。

思路:N为奇数,无解,因为(N-1)/2要么到N,要么到0,不能满足N和0同时被遍历。

所以考虑N是偶数,发现i和(i+N/2)%N连出去的边相同。 那么发现给个点的入度出度都为2,由于互斥关系,那么入度出度都是1,就是求欧拉回路了; 一定有解,dfs倒序输出,即可。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int Mod=1e9+;
int vis[maxn],ans[maxn],tot,N;
void dfs(int u)
{
if(vis[u]) return ;
vis[u]=;
dfs(u*%N);
dfs((u*+)%N);
ans[++tot]=u;
}
int main()
{
scanf("%d",&N);
if(N&) return puts("-1"),;
dfs();
for(int i=tot;i>=;i--) printf("%d ",ans[i]);
puts("");
return ;
}

CodeForces - 325E:The Red Button (哈密尔顿 转 欧拉回路)的更多相关文章

  1. poj 2288 Islands and Bridges_状态压缩dp_哈密尔顿回路问题

    题目链接 题目描述:哈密尔顿路问题.n个点,每一个点有权值,设哈密尔顿路为 C1C2...Cn,Ci的权值为Vi,一条哈密尔顿路的值分为三部分计算: 1.每一个点的权值之和 2.对于图中的每一条CiC ...

  2. Codeforces 325E

    Codeforces 325E 原题 题目描述:给出\(n\)个点, 编号为\(0 \text ~ n-1\),每个点连出两条边分别为\(2i\)和\(2i+1\)(\(mod n\)),现从\(0\ ...

  3. 旅行商问题(TSP)、最长路径问题与哈密尔顿回路之间的联系(归约)

    一,旅行商问题与H回路的联系(H回路 定义为 哈密尔顿回路) 旅行商问题是希望售货员恰好访问每个城市一次,最终回到起始城市所用的费用最低,也即判断图中是否存在一个费用至多为K的回路.(K相当于图中顶点 ...

  4. poj 2280 Islands and Bridges 哈密尔顿路 状压dp

    题目链接 题意 给定一个\(N\)个点的无向图,求一条哈密尔顿路径\(C_1C_2...C_n\),使其\(value\)最大. \(value\)的计算方式如下:\[\begin{aligned}v ...

  5. 哈密尔顿环x

    欧拉回路是指不重复地走过所有路径的回路,而哈密尔顿环是指不重复地走过所有的点,并且最后还能回到起点的回路.  代码如下: #include<iostream> #include<cs ...

  6. Codeforce 263D Cycle in Graph 搜索 图论 哈密尔顿环

    You've got a undirected graph G, consisting of n nodes. We will consider the nodes of the graph inde ...

  7. The Red Button

    The Red Button 问题 问题描述 Piegirl终于发现了红色按钮,你现在还剩最后一个机会去改变这个结局.这个按钮下面的电路由n个从0到n-1编号节点组成.为了关闭这个按钮,这n个节点必须 ...

  8. poj 2288 Islands and Bridges——状压dp(哈密尔顿回路)

    题目:http://poj.org/problem?id=2288 不知为什么记忆化搜索就是WA得不得了! #include<iostream> #include<cstdio> ...

  9. Codeforces Round #296 (Div. 1) C. Data Center Drama 欧拉回路

    Codeforces Round #296 (Div. 1)C. Data Center Drama Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xx ...

随机推荐

  1. Android中getLocationOnScreen和getLocationInWindow 获取屏幕大小

    需要确定组件在父窗体中的坐标时,使用getLocationInWindow,需要获得组件在整个屏幕的坐标时,使用getLocationOnScreen. 其中location [0]代表x坐标,loc ...

  2. smarty课程---smarty3的安装和使用

    smarty课程---smarty3的安装和使用 一.总结 一句话总结:smarty 是什么,就不多说了,用过php,接触过php的人都对smarty 再熟悉不过了.它是一个很强大的代码分离软件,作为 ...

  3. codeforces 494a//Treasure// Codeforces Round #282(Div. 1)

    题意:一个'('  ,  ')'  ,  '#'组成的串,可将'#'换成至少一个')'.问一个换法能使串匹配. 至少换成一个,那么就先都换成一个,记结果为str.最后一个')'的后面没有未匹配的'(' ...

  4. .net 外部CSS文件不起作用总结

    外部css文件样式全部不起作用 asp.net 页面引用路径的问题 缺少必须属性<link rel="stylesheet" type="text/css" ...

  5. springmvc基础流程

    转载:http://blog.csdn.net/zuoluoboy/article/details/19766131 注意:springmvc多个拦截器执行流程:每个拦截器的方法preHandler顺 ...

  6. tensorflow安装相关问题

    安装步骤 要求:Python必须是64位根据TensorFlow的计算方式,TensorFlow的安装分为CPU版本和GPU版本对于Python3.5或者Python3.6,可以使用pip insta ...

  7. Windows环境搭建ElasticSearch 5.*并配置head

    前言: ES5*以上版本需要jdk1.8,jdk1.8,jdk1.8.重要的事情说三遍 1.下载ElasticSearch https://www.elastic.co/cn/downloads/el ...

  8. office每次打开都要重新配置

    office每次打开都要重新配置     1● 找到路径 C:\Program Files\Common Files\microsoft shared\OFFICE14\Office Setup Co ...

  9. httpclient cookie相关介绍

    http状态管理 cookie是HTTP代理和目标服务器可以交流保持回话的状态信息的令牌或短包. httpclient使用Cookie接口来代表抽象的cookie令牌,在它的简单形式中http的coo ...

  10. SQL Server 调优系列基础篇 - 性能调优介绍

    前言 关于SQL Server调优系列是一个庞大的内容体系,非一言两语能够分析清楚,本篇先就在SQL 调优中所最常用的查询计划进行解析,力图做好基础的掌握,夯实基本功!而后再谈谈整体的语句调优. 通过 ...