HDOJ 5269 - ZYB loves Xor I

Source

BestCoder Round #44 2問目
HDOJ 5269

問題概要

$x$ を $2$ 進数で表記した時に,$1$ の桁の中で最も小さい桁が $2^k$ の桁だとすると,$f(x)$ を $f(x)=2^k$ で定義する.
ただし,$f(0)=0$ とする.
要素数 $N$ の整数列 $\{A_k\}_{k=1}^N$ が与えられるので,
$\qquad \displaystyle \sum_{i=1}^N \sum_{j=1}^N f(A_i\ {\rm XOR}\ A_j)$
の値を ${\rm mod}\ 998244353$ で求める問題.

解法

まず,$f(A_i\ {\rm XOR}\ A_j)=1$ となる $(A_i,A_j)$ のペアの数を求める.
これは,$2$ 進数で一番下の桁が $0$ か $1$ かで,$\{A_k\}$ を $2$ つのグループに分ける.
$A_i$ と $A_j$ を違うグループから取ってきた時のみ $f(A_i\ {\rm XOR}\ A_j)=1$ となるので,それぞれのグループのサイズからペアの数は求まる.
次に,各グループの中で,$f(A_i\ {\rm XOR}\ A_j)=2$ となるものを求める.
これは,各グループごとに,また,$2^1$ の桁が $0$ か $1$ かでグループに分ければわかる.
同様に,$2^k$ の値でどんどんグループを細かく分けていけば良い.
同じグループ内の整数が全ておなじになったら,残りの $f(A_i\ {\rm XOR}\ A_j)$ は $0$ になるので,そこで終了すれば良い.
時間計算量は,$d = \max A_k$ として,$O(N \log d)$ 程度.

C++によるスパゲッティなソースコード

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define mygc(c) (c)=getchar()
#define mypc(c) putchar(c)

#define ll long long
#define ull unsigned ll

void reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}

#define MD 998244353

int get_inv(ll a, int md){ll t=a,s=md,u=1,v=0,e;while(s){e=t/s;t-=e*s;u-=e*v;swap(t,s);swap(u,v);}if(u<0)u+=md;return u;}

struct mint{
  static unsigned md, W, R, Rinv, mdninv, RR;
  unsigned val;

  mint(){}mint(int a){val=mulR(a);}mint(unsigned a){val=mulR(a);}mint(ll a){val=mulR(a);}mint(ull a){val=mulR(a);}
  unsigned setmod(unsigned m){int i;unsigned t;W=32;md=m;R=(1ULL<<W)%md;RR=(ull)R*R%md;switch(m){case 104857601:Rinv=2560000;mdninv=104857599;break;case 998244353:Rinv=232013824;mdninv=998244351;break;case 1000000007:Rinv=518424770;mdninv=2226617417U;break;case 1000000009:Rinv=171601999;mdninv=737024967;break;case 1004535809:Rinv=234947584;mdninv=1004535807;break;case 1007681537:Rinv=236421376;mdninv=1007681535;break;case 1012924417:Rinv=238887936;mdninv=1012924415;break;case 1045430273:Rinv=254466304;mdninv=1045430271;break;case 1051721729:Rinv=257538304;mdninv=1051721727;break;default:Rinv=get_inv(R,md);mdninv=0;t=0;rep(i,W){if(t%2==0)t+=md,mdninv|=(1U<<i);t/=2;}}}
  unsigned mulR(unsigned a){return(ull)a*R%md;}unsigned mulR(int a){if(a<0)a=a%md+md;return mulR((unsigned)a);}unsigned mulR(ull a){return mulR((unsigned)(a%md));}unsigned mulR(ll a){if(a<0)a=a%md+md;return mulR((unsigned)a);}
  unsigned reduce(unsigned T){unsigned m=T*mdninv;unsigned t=(unsigned)((T+(ull)m*md)>>W);if(t>=md)t-=md;return t;}unsigned reduce(ull T){unsigned m=(unsigned)T*mdninv;unsigned t=(unsigned)((T+(ull)m*md)>>W);if(t>=md)t-=md;return t;}
  unsigned get(){return reduce(val);}
  mint&operator+=(mint a){val+=a.val;if(val>=md)val-=md;return*this;}mint&operator-=(mint a){if(val<a.val)val=val+md-a.val;else val-=a.val;return*this;}mint&operator*=(mint a){val=reduce((ull)val*a.val);return*this;}mint&operator/=(mint a){return*this*=a.inverse();}
  mint operator+(mint a){return mint(*this)+=a;}mint operator-(mint a){return mint(*this)-=a;}mint operator*(mint a){return mint(*this)*=a;}mint operator/(mint a){return mint(*this)/=a;}
  mint operator+(int a){return mint(*this)+=mint(a);}mint operator-(int a){return mint(*this)-=mint(a);}mint operator*(int a){return mint(*this)*=mint(a);}mint operator/(int a){return mint(*this)/=mint(a);}
  mint operator+(ll a){return mint(*this)+=mint(a);}mint operator-(ll a){return mint(*this)-=mint(a);}mint operator*(ll a){return mint(*this)*=mint(a);}mint operator/(ll a){return mint(*this)/=mint(a);}
  mint operator-(void){mint res;if(val)res.val=md-val;else res.val=0;return res;}
  operator bool(void){return val!=0;}operator int(void){return get();}operator ll(void){return get();}
  mint inverse(){int a=val,b=md,u=1,v=0,t;mint r;while(b){t=a/b;a-=t*b;swap(a,b);u-=t*v;swap(u,v);}if(u<0)u+=md;r.val=(ull)u*RR%md;return r;}
  mint pw(ull b){mint a(*this),r;r.val=R;while(b){if(b&1)r*=a;b>>=1;a*=a;}return r;}
};
unsigned mint::md, mint::W, mint::R, mint::Rinv, mint::mdninv, mint::RR;
mint operator+(int a, mint b){return mint(a)+=b;}mint operator-(int a, mint b){return mint(a)-=b;}mint operator*(int a, mint b){return mint(a)*=b;}mint operator/(int a, mint b){return mint(a)/=b;}
mint operator+(ll a, mint b){return mint(a)+=b;}mint operator-(ll a, mint b){return mint(a)-=b;}mint operator*(ll a, mint b){return mint(a)*=b;}mint operator/(ll a, mint b){return mint(a)/=b;}

mint mval[100000], minv[100000];
void mint_init(int md=MD, mint val[]=mval, int vals=100000, mint inv[]=minv, int invs=100000){int i;val[0].setmod(md);val[0].val=0;REP(i,1,vals){val[i].val=val[i-1].val+mint::R;if(val[i].val >=md)val[i].val-=md;}inv[1].val=1;REP(i,2,invs){inv[i].val=md-((ll)(md/i)*inv[md%i].val%md);}REP(i,1,invs)inv[i].val=(ull)inv[i].val*mint::R%md;}


int T;
int N, A[100000];
int aa[100000], bb[100000];

mint solve(int st, int ed, int bit){
  int i, as, bs;
  int mid;
  mint res = mval[0];

  REP(i,st,ed) if(A[i]!=A[st]) break;
  if(i==ed) return 0;

  as = bs = 0;
  REP(i,st,ed){
    if(A[i]&bit) bb[bs++] = A[i]; else aa[as++] = A[i];
  }
  rep(i,as) A[st+i] = aa[i];
  rep(i,bs) A[st+as+i] = bb[i];

  REP(i,st,ed) if(A[i]&bit) break;
  mid = i;

  if(mid==st || mid==ed) return solve(st,ed,bit*2);

  res = solve(st, mid, bit*2) + solve(mid, ed, bit*2);
  res += mint(bit) * mval[mid-st] * mval[ed-mid];
  return res;
}

int main(){
  int i;
  int Case = 0;
  mint res;

  mint_init();

  reader(&T);
  while(T--){
    reader(&N);
    rep(i,N) reader(A+i);
    res = solve(0, N, 1) * mval[2];
    printf("Case #%d: %d\n",++Case,(int)res);
  }

  return 0;
}

Current time: 2017年07月23日15時47分07秒
Last modified: 2015年06月17日03時47分33秒 (by laycrs)
Tags: Competitive_Programming Hangzhou_Dianzi_University_Online_Judge BestCoder_44 BestCoder_B
トップページに戻る

Logged in as: unknown user (not login)

ログイン: