1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; typedef long long LL; inline const LL Get_Int() { LL num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxt=100005*30; struct Tree { int child[2],cnt; void clear() { child[0]=child[1]=cnt=0; } }; struct Trie { Tree tree[maxt]; #define ch(x,i) tree[x].child[i] int cnt,root; void init() { cnt=0; tree[1].clear(); root=++cnt; } void insert(int num) { int x=root; for(int i=30; i>=0; i--) { bool y=num&(1<<i); if(ch(x,y)==0) { ch(x,y)=++cnt; tree[ch(x,y)].clear(); } x=ch(x,y); } tree[x].cnt++; } pair<int,int>find(int num) { int x=root,ans=0; for(int i=30; i>=0; i--) { bool y=num&(1<<i),chose; if(ch(x,y))chose=y; else chose=!y; ans|=chose<<i; x=ch(x,chose); } return make_pair(ans^num,tree[x].cnt); } } trie; const int maxn=100005; const LL mod=1e9+7; LL n,a[maxn],l[maxn],r[maxn],ans=0,ans2=1; LL Quick_Pow(LL a,LL b) { LL ans=1; while(b) { if(b&1)ans=ans*a%mod; a=a*a%mod; b>>=1; } return ans; } void Binary(int Left,int Right,int depth) { if(Left>=Right)return; if(depth<0) { ans2=ans2*Quick_Pow(Right-Left+1,Right-Left-1)%mod; return; } int lpos=0,rpos=0; for(int i=Left; i<=Right; i++) if(a[i]&(1<<depth))l[++lpos]=a[i]; else r[++rpos]=a[i]; for(int i=1; i<=lpos; i++)a[Left+i-1]=l[i]; for(int i=1; i<=rpos; i++)a[Left+lpos+i-1]=r[i]; if(lpos&&rpos) { LL sum=0x7fffffff/2,cnt=0; trie.init(); for(int i=1; i<=lpos; i++)trie.insert(l[i]); for(int i=1; i<=rpos; i++) { pair<int,int>tmp=trie.find(r[i]); if(tmp.first<sum) { sum=tmp.first; cnt=tmp.second; } else if(tmp.first==sum)cnt+=tmp.second; } ans+=sum; ans2=ans2*cnt%mod; } Binary(Left,Left+lpos-1,depth-1); Binary(Left+lpos,Right,depth-1); } int main() { n=Get_Int(); for(int i=1; i<=n; i++)a[i]=Get_Int(); Binary(1,n,30); printf("%lld\n%lld\n",ans,ans2); return 0; }
|