Number Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1886 Accepted Submission(s): 561
Special Judge
Problem Description
There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules:
● ai ∈ [0,n]
● ai ≠ aj( i ≠ j )
For sequence a and sequence b, the integrating degree t is defined as follows(“⊕” denotes exclusive or):
t = (a0 ⊕ b0) + (a1 ⊕ b1) +···+ (an ⊕ bn)
(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.
● ai ∈ [0,n]
● ai ≠ aj( i ≠ j )
For sequence a and sequence b, the integrating degree t is defined as follows(“⊕” denotes exclusive or):
t = (a0 ⊕ b0) + (a1 ⊕ b1) +···+ (an ⊕ bn)
(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 ≤ 105), The second line contains a0,a1,a2,...,an.
For each case, the first line contains an integer n(1 ≤ n ≤ 105), The second line contains a0,a1,a2,...,an.
Output
For each case, output two lines.The first line contains the maximum integrating degree t. The second line contains n+1 integers b0,b1,b2,...,bn. There is exactly one space between bi and bi+1(0 ≤ i ≤ n - 1). Don’t ouput any spaces after bn.
Sample Input
4
2 0 1 4 3
Sample Output
20
1 0 2 3 4
题意:
给出 N ,后给出 N 个数,它是一个 0 ~ N 的排列,找出一个序列对应式子异或和最大。
思路:
将数变为二进制考虑,会发现每个数都会有对应有一个 “ 使之变为该二进制位数全为 1 ” 的数。这样子就能解决问题了,总和就是化为全为 1 时候对应的十进制值,对应的数就是这个和减去这个值的得数。
AC:
#include <cstdio> #include <cstring> #include <algorithm> #include <set> #include <cmath> using namespace std; typedef long long ll; ll num[100005]; ll num1[100005]; ll Bit (ll ans) { ll bb = 0; while (ans) { ++bb; ans /= 2; } return bb; } int main() { int n; while (~scanf("%d", &n)) { for (int i = 0; i <= n; ++i) { scanf("%I64d", &num[i]); } memset(num1, -1, sizeof(num1)); ll sum = 0; for (ll i = n; i >= 0; --i) { if (num1[i] == -1) { ll bb = Bit(i); ll ans = pow(2, bb) - 1; sum += ans * 2; num1[i] = ans - i; num1[ans - i] = i; } } if (num1[0] == -1) num1[0] = 0; printf("%I64d\n", sum); for (int i = 0; i <= n; ++i) { printf("%I64d", num1[num[i]]); i == n ? printf("\n") : printf(" "); } } return 0; }
相关推荐
二进制和运算符 二进制计数设置位 二进制计数尾随零 二进制或运算符 二进制移位 二进制二进制补码 二进制异或运算符 计数 1S 布赖恩·克尼汉方法 计算 1 位数 格雷码序列 最高设置位 最右边设置位的索引...
服务端自动生成序列号(SequenceNumber) 4.全中文解析及二进制格式包内容显示 中国电信小灵通SMGP模拟器: 概述:基于最新的SMGP v3.0 v2.0协议开发,增加了对交易请求消息的支持,具有方便易用图形化的界面,专业...
Oracle P/L SQL实现发送Email、浏览网页等网络操作功能... --十进制转换成三十二进制函数 --Select UTL_INet.f_Dec2Hex( 187 ) From dual; Function f_Dec2Hex( an_Dec in Number )Return VarChar2; --三十二...
" "REAL、FLOAT、INTEGER、NUEBER "Oracle数据库必须使用NUEBER " "NUMBER(P,S)、NUMERIC (P, "Oracle数据库必须使用NUMBER " "S)、DECIMAL (P, S) " " "DATE "时间类型必须使用DATE " "BLOB(二进制数据)、CLOB...
二进制数据类型 row 1~2000字节 可变长二进制数据,在具体定义字段的时候必须指明最大长度n long raw 1~2GB 可变长二进制数据 LOB数据类型 clob 1~4GB 只能存储字符数据 nclob 1~4GB 保存本地语言字符集数据 blob...
上面的语法格式中,sequence就是一个集合对象,也可以是一个表达式,但该表达式将返回一个集合对象,而item是一个任意的名字,就是被迭代输出的集合元素.此外,迭代集合对象时,还包含两个特殊的循环变量: item_index:...
<3>.pctfree(index)=(maximum number of rows-initial number of rows)*100/maximum number of rows <4>.creating reverse key indexes sql> create unique index xay_id on xay(a) reverse pctfree 30 ...
RFC856 Telnet二进制传输 RFC857 Telnet回声选项 RFC858 Telnet抑制前进选项 RFC859 Telnet状态选项 RFC860 Telnet定时标记选项 RFC861 Telnet扩展选项列表选项 RFC862 回声协议 RFC863 废除协议 RFC864 字符产生...
RFC856 Telnet二进制传输 RFC857 Telnet回声选项 RFC858 Telnet抑制前进选项 RFC859 Telnet状态选项 RFC860 Telnet定时标记选项 RFC861 Telnet扩展选项列表选项 RFC862 回声协议 RFC863 废除协议 RFC864 字符产生...
RFC856_Telnet二进制传输 RFC857_Telnet回声选项 RFC858_Telnet抑制前进选项 RFC859_Telnet状态选项 RFC860_Telnet定时标记选项 RFC861_Telnet扩展选项列表选项 RFC862_回声协议 RFC863 废除协议 RFC864 字符产生...