#include <iostream> #include <cstdio> #include <vector> #include <utility> #include <set> #include <queue> #include <cstring> #include <algorithm> using namespace std; #define SZ(x) ((int)(x).size()) typedef pair<int,int> pii; const int MAX_N = 1e5 +6; int prime[MAX_N]; void build() { prime[0] = prime[1] = 1; for (int i=2;MAX_N>i;i++) { if (prime[i] == 0) { prime[i]=i; for (long long j=i*1LL*i;MAX_N>j;j+=i) { prime[j] = i; } } } } vector<pii> get_prime_divisor(int x) { vector<pii> ret; while (x!=1) { pii tmp; tmp.first = prime[x]; int cnt=0; int pp=prime[x]; while (x%pp==0) { x/=pp; cnt++; } tmp.second = cnt; ret.emplace_back(tmp); } return ret; } vector<int> get_divisor(int x) { vector<pii> prime_divisor=get_prime_divisor(x); int sz=SZ(prime_divisor); queue<pii> que; que.push({1,0}); vector<int> ret; while (!que.empty()) { pii p=que.front(); que.pop(); if (p.second == sz) { ret.emplace_back(p.first); continue; } int t=1; for (int i=0;prime_divisor[p.second].second>=i;i++) { que.push(make_pair(p.first*t,p.second+1)); t*=prime_divisor[p.second].first; } } return ret; } int dp[MAX_N]; const int MAGIC = 60; void doo(int id) { vector<int> ret=get_divisor(id); int cnt[MAGIC]; sort(ret.begin(),ret.end()); memset(cnt,0,sizeof(cnt)); for (int i:ret) { if (i==id) continue; if (dp[i] == -1) doo(i); cnt[((id/i)%2==0?0:dp[i])]=1; } for (int i=0;MAGIC>i;i++) { if (!cnt[i]) { dp[id] = i; break; } } } int main () { build(); int T; scanf("%d",&T); memset(dp,-1,sizeof(dp)); dp[1] = 0; while (T--) { int n; scanf("%d",&n); int ans=0; for (int i=1;n>=i;i++) { int h; scanf("%d",&h); if (dp[h] != -1) ans ^= dp[h]; else { doo(h); ans ^= dp[h]; } } if (ans) puts("1"); else puts("2"); } }
2017年9月7日 星期四
(Hackerrank) Tower Breakers, Again!
https://www.hackerrank.com/challenges/kitty-and-katty
(Hackerrank) Tower Breakers, Revisited!
https://www.hackerrank.com/challenges/tower-breakers-revisited-1
SG value
SG value
#include <iostream> #include <cstdio> #include <vector> #include <utility> #include <set> #include <queue> #include <cstring> using namespace std; #define SZ(x) ((int)(x).size()) typedef pair<int,int> pii; const int MAX_N = 1e6 +6; int prime[MAX_N]; void build() { prime[0] = prime[1] = 1; for (int i=2;MAX_N>i;i++) { if (prime[i] == 0) { prime[i]=i; for (long long j=i*1LL*i;MAX_N>j;j+=i) { prime[j] = i; } } } } vector<pii> get_prime_divisor(int x) { vector<pii> ret; while (x!=1) { pii tmp; tmp.first = prime[x]; int cnt=0; int pp=prime[x]; while (x%pp==0) { x/=pp; cnt++; } tmp.second = cnt; ret.emplace_back(tmp); } return ret; } vector<int> get_divisor(int x) { vector<pii> prime_divisor=get_prime_divisor(x); int sz=SZ(prime_divisor); queue<pii> que; que.push({1,0}); vector<int> ret; while (!que.empty()) { pii p=que.front(); que.pop(); if (p.second == sz) { ret.emplace_back(p.first); continue; } int t=1; for (int i=0;prime_divisor[p.second].second>=i;i++) { que.push(make_pair(p.first*t,p.second+1)); t*=prime_divisor[p.second].first; } } return ret; } int dp[MAX_N]; const int MAGIC = 30; void doo(int id) { vector<int> ret=get_divisor(id); int cnt[MAGIC]; memset(cnt,0,sizeof(cnt)); for (int i:ret) { if (i==id) continue; if (dp[i] == -1) doo(i); cnt[dp[i]]=1; } for (int i=0;MAGIC>i;i++) { if (!cnt[i]) { dp[id] = i; break; } } } int main () { build(); int T; scanf("%d",&T); memset(dp,-1,sizeof(dp)); dp[1] = 0; while (T--) { int n; scanf("%d",&n); int ans=0; for (int i=1;n>=i;i++) { int h; scanf("%d",&h); if (dp[h] != -1) ans ^= dp[h]; else { doo(h); ans ^= dp[h]; } } if (ans) puts("1"); else puts("2"); } }
2017年9月6日 星期三
(Hackerrank) Balanced Forest
https://www.hackerrank.com/challenges/balanced-forest/problem
Treap + 啟發式合併
Treap + 啟發式合併
#include <iostream> #include <cstdio> #include <vector> #include <ctime> #include <cstdlib> #include <algorithm> using namespace std; typedef long long LL; const LL INF = 1e17 + 6; const int MAX_P = 1e6 + 6; struct Treap { static Treap mem[MAX_P]; Treap *lc,*rc; LL key; int pri,sz; Treap(){ } Treap(LL _key) : key(_key),pri(rand()),sz(1),lc(NULL),rc(NULL) { } } Treap::mem[MAX_P],*ptr=Treap::mem; int Sz(const Treap* t) { return t?t->sz:0; } void pull(Treap* t) { if (!t) return; t->sz = Sz(t->lc) + Sz(t->rc) + 1; } Treap* merge(Treap* a,Treap* b) { if (!a || !b) return a?a:b; else if (a->pri > b->pri) { a->rc=merge(a->rc,b); pull(a); return a; } else { b->lc=merge(a,b->lc); pull(b); return b; } } void split(Treap* t,LL k,Treap* &a,Treap* &b) { if (!t) a=b=NULL; else if (t->key <= k) { a=t; split(t->rc,k,a->rc,b); pull(a); } else { b=t; split(t->lc,k,a,b->lc); pull(b); } } bool ffind(Treap* &t,LL x) { Treap *tl,*tr; split(t,x-1,tl,t); split(t,x,t,tr); bool ret=(Sz(t)!=0); t=merge(tl,merge(t,tr)); return ret; } const int MAX_N = 5e4 +6; vector<int> edg[MAX_N]; LL a[MAX_N]; LL sz[MAX_N]; LL ans; void dfs1(int id,int par) { sz[id] = a[id]; for (int i:edg[id]) { if (i!=par) { dfs1(i,id); sz[id] += sz[i]; } } } void addd(Treap* &t,LL val) { Treap *tl; split(t,val,tl,t); t=merge(merge(tl,new(++ptr)Treap(val)),t); } void dell(Treap* &t,LL val) { Treap *tl,*tr; split(t,val-1,tl,t); split(t,val,t,tr); t=merge(merge(tl,t->lc),merge(t->rc,tr)); } int n; LL tot=0; Treap* root; LL get_ans(LL x,LL n_2x) { if (x<n_2x) return INF; else return x-n_2x; } void dfs2(int id,int par) { //sz[id] --> x //see have 2*x or tot-x if (ffind(root,2*sz[id])) { ans = min(ans,get_ans(sz[id],tot-2*sz[id])); } if (ffind(root,tot-sz[id])) { ans = min(ans,get_ans(sz[id],tot-2*sz[id])); } //sz[id] --> tot-2*x //see have tot-x if ((tot-sz[id])%2==0) { LL x=(tot-sz[id])/2; if (ffind(root,tot-x)) { ans = min(ans,get_ans(x,tot-2*x)); } } addd(root,sz[id]); for (int i:edg[id]) { if (i!=par) { dfs2(i,id); } } dell(root,sz[id]); } void split_by_sz(Treap* t,int k,Treap* &a,Treap* &b) { if (!t) a=b=NULL; else if (Sz(t->lc) + 1 <= k) { a=t; split_by_sz(t->rc,k-Sz(t->lc)-1,a->rc,b); pull(a); } else { b=t; split_by_sz(t->lc,k,a,b->lc); } } void ddfs(Treap* &a,Treap* t) { LL now=t->key; //now --> x //find x or tot-2*x if (ffind(a,now)) { ans = min(ans,get_ans(now,tot-2*now)); } if (ffind(a,tot-2*now)) { ans = min(ans,get_ans(now,tot-2*now)); } //now --> tot-2*x; //find x if ((tot-now)%2 == 0) { LL x=(tot-now)/2; if (ffind(a,x)) { ans = min(ans,get_ans(x,tot-2*x)); } } if (t->lc) ddfs(a,t->lc); if (t->rc) ddfs(a,t->rc); } void mmerge(Treap* &a,Treap* &b) { ddfs(a,b); while(Sz(b)) { Treap* t; split_by_sz(b,1,t,b); Treap *tl; split(a,t->key-1,a,tl); a=merge(merge(a,t),tl); } } Treap* sagiri[MAX_N]; void dfs3(int id,int par) { Treap* ret=NULL; vector<Treap*> v; for (int i:edg[id]) { if (i!=par) { dfs3(i,id); Treap* rett=sagiri[i]; if (Sz(rett) > Sz(ret)) { swap(rett,ret); } if (rett != NULL)v.push_back(rett); } } for (auto i:v) { mmerge(ret,i); } addd(ret,sz[id]); sagiri[id] = ret; } int main () { int T; scanf("%d",&T); while (T--) { ptr=Treap::mem; scanf("%d",&n); for (int i=0;n>=i;i++) { edg[i].clear(); } for (int i=1;n>=i;i++) { scanf("%lld",&a[i]); } for (int i=1;n-1>=i;i++) { int x,y; scanf("%d %d",&x,&y); edg[x].push_back(y); edg[y].push_back(x); } ans = INF; dfs1(1,1); root=NULL; tot=sz[1]; dfs2(1,1); dfs3(1,1); if (ans == INF) ans=-1; printf("%lld\n",ans); } }
2017年9月1日 星期五
(Hackerrank) Kitty's Calculations on a Tree [重心剖分]
https://www.hackerrank.com/challenges/kittys-calculations-on-a-tree
重心剖分 (centroid decomposition)
重心剖分 (centroid decomposition)
#include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <utility> using namespace std; typedef long long LL; typedef pair<LL,LL> pii; const int MAX_N = 2e5 + 6; const int MAX_P = 19; const LL mod = 1e9 + 7; vector<int> edg[MAX_N]; int dis[MAX_P][MAX_N]; bool visit[MAX_N]; struct Cen { int par; int depth; pii val_v_av; //first --> val, second --> minus pii val_v; } cen[MAX_N]; vector<int> v; int sz[MAX_N]; int mx[MAX_N]; void dfs2(int id) { v.push_back(id); visit[id]=1; sz[id]=1; mx[id]=0; for (int i:edg[id]) { if (!visit[i]) { dfs2(i); sz[id] += sz[i]; } } } #define SZ(x) ((int)(x).size()) int get_cen(int id) { v.clear(); dfs2(id); int tot=SZ(v); int cen=-1; for (int i:v) { if (max(mx[i],tot-sz[i]) <= tot/2) { cen=i; } visit[i]=false; } return cen; } void dfs3(int id,int par,int cen_depth,int dist) { dis[cen_depth][id] = dist; for (int i:edg[id]) { if (!visit[i] && i!=par) { dfs3(i,id,cen_depth,dist+1); } } } void dfs(int id,int cen_par,int cen_depth) { int ccen=get_cen(id); dfs3(ccen,ccen,cen_depth,0); cen[ccen]={cen_par,cen_depth,{0,0},{0,0}}; visit[ccen]=1; for (int i:edg[ccen]) { if (!visit[i]) dfs(i,ccen,cen_depth+1); } } pii operator+(const pii &p1,const pii &p2) { return make_pair(p1.first+p2.first,p1.second+p2.second); } pii operator-(const pii &p1,const pii &p2) { return make_pair(p1.first-p2.first,p1.second-p2.second); } pii operator+=(pii &p1,const pii &p2) { p1 = p1 + p2; return p1; } pii operator-=(pii &p1,const pii &p2) { p1 = p1 - p2; return p1; } void Pure(pii &p) { p.first = (p.first%mod + mod) % mod; p.second = (p.second%mod + mod) % mod; } void addd(LL x) { LL p=x; while (p!=-1) { cen[p].val_v += {x,0}; cen[p].val_v_av += {x*dis[cen[p].depth][x],0}; if (cen[p].par != -1) { int par=cen[p].par; cen[p].val_v -= {0,x}; cen[p].val_v_av -= {0,x*dis[cen[par].depth][x]}; } Pure(cen[p].val_v); Pure(cen[p].val_v_av); p=cen[p].par; } } void dell(LL x) { LL p=x; while (p!=-1) { cen[p].val_v -= {x,0}; cen[p].val_v_av -= {x*dis[cen[p].depth][x],0}; if (cen[p].par != -1) { int par=cen[p].par; cen[p].val_v += {0,x}; cen[p].val_v_av += {0,x*dis[cen[par].depth][x]}; } Pure(cen[p].val_v); Pure(cen[p].val_v_av); p=cen[p].par; } } LL query(LL x) { LL ret=0; LL v=0; LL v_av=0; int p=x; while (p!=-1) { v += cen[p].val_v.first; v_av += cen[p].val_v_av.first; ret += x*v_av; ret %= mod; ret += x*dis[cen[p].depth][x]*v; ret %= mod; v = cen[p].val_v.second; v_av = cen[p].val_v_av.second; p=cen[p].par; } return ret; } LL pow(LL a,LL n,LL mod) { if (n==0) return 1; else if (n==1) return a; LL ret=pow(a,n/2,mod); ret*=ret; ret%=mod; if (n&1) { ret*=a; ret%=mod; } return ret; } int main () { int n,q; scanf("%d %d",&n,&q); for (int i=1;n-1>=i;i++) { int a,b; scanf("%d %d",&a,&b); edg[a].push_back(b); edg[b].push_back(a); } dfs(1,-1,0); while (q--) { int k; scanf("%d",&k); vector<int> v; while (k--) { int x; scanf("%d",&x); v.push_back(x); } for (int i:v) addd(i); LL ans=0; for (int i:v) { ans += query(i); ans%=mod; } for (int i:v) dell(i); printf("%lld\n",(ans*pow(2,mod-2,mod) + mod)%mod); } }
訂閱:
文章 (Atom)