Number Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1192 Accepted Submission(s): 525
Special Judge
Problem Description
There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules:
● a i ∈ [0,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.
● a i ∈ [0,n]
● a i ≠ a j( i ≠ j )
For sequence a and sequence b, the integrating degree t is defined as follows(“⊕” denotes exclusive or):
(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.
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
iand 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 <stdio.h>
#include <string.h>
#define ll __int64
#define M 100115
ll ans;
int map[]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535,131071};
//打个小小的表,表示二进制为1,11,111,1111等类似的在100000范围内的数
int a[M],b[M];
int main(){
int i,j,n,m,x;
while(~scanf("%d",&n)){
int flag=0;
memset(b,0,sizeof(b));
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
for(i=0;i<18;i++){
if(n<map[i]){
x=map[i];
break;
}
if(n==map[i]){
flag=1;
break;
}
}
if(flag){
ans=0;
for(i=0;i<=n;i++)
ans+=(n-a[i])^a[i];
printf("%I64d\n%d",ans,n-a[0]);
for(i=1;i<=n;i++)
printf(" %d",n-a[i]);
printf("\n");
continue;
}
int w=n;
ans=0;
while(w){
int s=x-w;
for(i=s;i<=w;i++)
b[i]=x-i;
w=s-1;
if(w<=0)
break;
flag=0;
for(i=0;i<18;i++){
if(w<map[i]){
x=map[i];
break;
}
if(w==map[i]){
flag=1;
break;
}
}
if(flag){
for(i=0;i<=w;i++)
b[i]=w-i;
break;
}
}
ans=0;
for(i=0;i<=n;i++)
ans+=a[i]^b[a[i]];
printf("%I64d\n%d",ans,b[a[0]]);
for(i=1;i<=n;i++)
printf(" %d",b[a[i]]);
printf("\n");
}
return 0;
}