There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules:

● a i ∈ 0,n0,n

● a i ≠ a j( i ≠ j )

For sequence a and sequence b, the integrating degree t is defined as follows(“�” denotes exclusive or):

t = (a 0 � b 0) + (a 1 � b 1) +・・・+ (a n � b n)

(sequence B should also satisfy the rules described above)

Now give you a number n and the sequence a. You should calculate the maximum integrating degree t and print the sequence b.

Input

There are multiple test cases. Please process till EOF.

For each case, the first line contains an integer n(1 ≤ n ≤ 10 5), The second line contains a 0,a 1,a 2,…,a n.

Output

For each case, output two lines.The first line contains the maximum integrating degree t. The second line contains n+1 integers b 0,b 1,b 2,…,b n. There is exactly one space between b i and b i+1 (0 ≤ i ≤ n - 1). Don’t ouput any spaces after b n.

Sample Input

4

2 0 1 4 3

Sample Output

20

1 0 2 3 4

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include <ctype.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; typedef long long ll;
const int maxn=110000;
const int INF=0x3f3f3f3f; ll a[maxn];
ll d[maxn];
int main()
{
ll n;
while(~scanf("%I64d",&n))
{
for(ll i=0; i<=n; i++)
scanf("%I64d",&a[i]);
memset(d,-1,sizeof(d));
ll ans=0;
for(ll i=n; i>=0; i--)
{
ll t=0;
if(d[i]==-1)
{
for(ll j=0;; j++)
{
if(!(i&(1<<j))) t+=(1<<j);
if(t>=i)
{
t-=(1<<j);
break;
}
}
ans+=(i^t)*2;
d[i]=t;
d[t]=i;
}
}
printf("%I64d\n",ans);
for(ll i=0; i<=n; i++)
printf(i==n?"%I64d\n":"%I64d ",d[a[i]]);
}
return 0;
}

Number Sequence HDU - 5014的更多相关文章

  1. (KMP 模板)Number Sequence -- Hdu -- 1711

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...

  2. AC日记——Number Sequence hdu 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. Number Sequence(HDU 1005 构造矩阵 )

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. Number Sequence HDU 1711(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...

  5. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

  6. Number Sequence HDU 1711 KMP 模板

    题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...

  7. Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据

    Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Pro ...

  8. ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

    Description There is a special number sequence which has n+1 integers. For each number in sequence, ...

  9. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

随机推荐

  1. HTTP响应码摘自apach官网

    HTTP状态列表 响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行. 响应码分五种类型,由它们的第一位数字表示: 1xx:信息,请求收到,继续处理 2xx:成功,行为被成功地接 ...

  2. 通过or注入py脚本

    代码思路 1.主要还是参考了别人的代码,确实自己写的和别人写的出路很大,主要归咎还是自己代码能力待提高吧. 2.将功能集合成一个函数,然后通过*args这个小技巧去调用.函数的参数不是argv的值,但 ...

  3. [Linux]Linux printf 输出重定向【转】

    转自:http://www.cnblogs.com/aaronLinux/p/6765145.html?utm_source=itdadao&utm_medium=referral 方法一 # ...

  4. python基础===一道小学奥数题的解法

    今早在博客园和大家分享了一道昨晚微博中看到的小学奥数题,后来有朋友给出了答案.然后我尝试用python解答它. 原题是这样的: 数学题:好事好 + 要做好 = 要做好事,求 “好.事.做.要”的值分别 ...

  5. Redis 分片实现 Redis Shard [www]

    Redis 分片实现                                             Redis Shard https://www.oschina.net/p/redis-s ...

  6. FineReport——JS二次开发(自定义翻页按钮)

    FR允许自定义工具栏上面的按钮,并提交JS方法: 对于翻页功能,大概有首页,下一页,上一页,最后一页,以及跳转页等功能. 不得不说的是,在HTML页面自定义的按钮如何获取到报表模板,通过FR提供的JS ...

  7. C++变量类型转换

    1:int转换为CString CString str; str.Format("As string: %d", int); 2:double转换为CString CString ...

  8. POJ 1160 Post Office(DP+经典预处理)

    题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...

  9. golang-指针,函数,map

    指针 普通类型变量存的就是值,也叫值类型.指针类型存的是地址,即指针的值是一个变量的地址.一个指针只是值所保存的位置,不是所有的值都有地址,但是所有的变量都有.使用指针可以在无需知道变量名字的情况下, ...

  10. python接口自动化13-data和json参数傻傻分不清【转载】

    转至博客:上海-悠悠 前言 在发post请求的时候,有时候body部分要传data参数,有时候body部分又要传json参数,那么问题来了:到底什么时候该传json,什么时候该传data? 一.识别j ...