fork download
  1. #include <iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  7.  
  8. typedef long long int ll;
  9.  
  10. vector<ll>vec;
  11.  
  12. int main(int argc, char** argv) {
  13. ll n,i;
  14. ll ele;
  15. cin>>n;
  16. for(i=0;i<n;i++)
  17. {
  18. cin>>ele;
  19. vec.push_back(ele);
  20. }
  21.  
  22. sort(vec.rbegin(),vec.rend());
  23.  
  24. ll row=0;
  25. // find the msb of the largest number
  26.  
  27. ll msb=1,res=0;
  28.  
  29. while(msb<=vec[0])
  30. msb<<=1;
  31.  
  32. msb>>=1;
  33.  
  34. for(ll row=0; msb>=1; msb>>=1)
  35. {
  36. ll temp=row;
  37.  
  38. while(temp<n && (vec[temp]&msb)==0)
  39. temp++;
  40.  
  41. if(temp>=n)
  42. continue;
  43.  
  44. swap(vec[row],vec[temp]);
  45.  
  46. for(ll j=0;j<n;j++)
  47. {
  48. if(j!=row && (vec[j]&msb)!=0)
  49. vec[j]= vec[j]^vec[row];
  50.  
  51. cout<<vec[j]<<" ";
  52. }
  53. cout<<endl;
  54. row++;
  55. }
  56.  
  57. for(i=0;i<n;i++)
  58. {
  59. // uncomment here
  60. // cout<<res<<" ";
  61. res^=vec[i];
  62. }
  63.  
  64. cout<<endl<<res<<endl;
  65.  
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0s 3480KB
stdin
3
3 10 9
stdout
10 3 3 
9 3 0 

10