HDU 5014 Number Sequence(位运算)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014
解题报告:西安网赛的题,当时想到一半,只想到从大的开始匹配,做异或运算得到对应的b[i],但是少了一个操作,ans[i] = temp,却没有想到ans[temp] = i;所以就一直卡在这里了,因为我不确定这样是不是一一对应的,好吧,也没有想到这里,就差这么点了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 typedef __int64 LL; 7 const int maxn = 100005; 8 int a[maxn],ans[maxn],visit[maxn]; 9 int get(int d) 10 { 11 int num = 0; 12 while(d) 13 { 14 d >>= 1; 15 num++; 16 } 17 return num; 18 } 19 20 int main() 21 { 22 int n; 23 while(scanf("%d",&n)!=EOF) 24 { 25 for(int i = 0;i <= n;++i) 26 scanf("%d",&a[i]); 27 memset(visit,0,sizeof(visit)); 28 memset(ans,0,sizeof(ans)); 29 for(int i = n;i > 0;--i) 30 { 31 if(visit[i]) continue; 32 int d = get(i); 33 int temp = ((1 << d) - 1) ^ i; 34 ans[i] = temp; 35 ans[temp] = i; 36 visit[temp] = visit[i] = 1; 37 } 38 LL sum = 0; 39 for(int i = 0;i <= n;++i) 40 sum += (LL)(i ^ ans[i]); 41 printf("%I64d\n",sum); 42 for(int i = 0;i <= n;++i) 43 printf(i == n? "%d\n":"%d ",ans[a[i]]); 44 } 45 return 0; 46 }