Number Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2044 Accepted Submission(s): 798
Special Judge
● ai ∈ [0,n]
● ai ≠ aj( 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.
For each case, the first line contains an integer n(1 ≤ n ≤ 105), The second line contains a0,a1,a2,...,an.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
int n,b[100000+10],a,ans[100000+10];
while(~scanf("%d",&n)) {
memset(ans,0,sizeof(ans));
long long sum=0;
for(int i=n;i>=0;--i) {
if(ans[i]==0) {
int t=i,k=0;
while(t) {
b[k]=t%2;
k++;
t/=2;
}
for(int j=0;j<k;++j) {
b[j]=!b[j];
ans[i]+=b[j]*pow(2,j);
}
ans[ans[i]]=i;
sum+=ans[i]^i;
}
}
printf("%I64d\n",sum*2);
for(int i=0;i<=n;i++) {
scanf("%d",&a);
if(i==0) {
printf("%d",ans[a]);
}
else
printf(" %d",ans[a]);
}
printf("\n");
}
return 0;
}