今晚best code第二题
好好学了下字典树,确实自己会的东西实在太少了
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstdlib> #include<queue> #include<map> #include<stack> #include<list> #include<vector> #include<cstring> #define inf 1<<30 #define Inf -1<<30 #define maxn 50000+5 #define ll long long #define mod 998244353 #define uli unsigned long int #define f_(i,x,n) for(int i=x;i<=n;i++) #define F_(i,x,n) for(int i=x;i>=n;i--) using namespace std; int n; struct stu { int sum; stu *next[2]; stu() { sum=0; next[0]=next[1]=NULL; } }; void add(stu*root,int x) { f_(i,0,30) { int m=x%2; x/=2; if(root->next[m]==NULL) { root->next[m]=new stu(); } root=root->next[m]; root->sum++; } } ll solve(stu*root,int cnt) { ll re=0,l,r; if(root->next[0]==NULL) l=0; else { l=root->next[0]->sum; re=(re+solve(root->next[0],cnt+1))%mod; } if(root->next[1]==NULL) r=0; else { r=root->next[1]->sum; re=(re+solve(root->next[1],cnt+1))%mod; } re+=l*r*(1<<cnt)%mod; return re; } int main() { int casee=1; cin.sync_with_stdio(false); int t; cin>>t; while(t--) { cin>>n; stu *root=new stu(); f_(i,0,n-1) { int x; cin>>x; add(root,x); } cout<<"Case #"<<casee++<<": "; cout<<solve(root,0)*2%mod<<endl; } return 0; }
时间: 2024-11-13 17:23:13