2016 计蒜之道 初赛 第一场 D 青云的机房组网方案 (虚树)
大意: 给定树, 点$i$的点权为$a_i$, 求$\sum\limits_{a_i \perp a_j}dis(i,j)$
中等难度可以枚举每条边的贡献, 维护子树内每个数出现次数$a$, 转化为求$\sum\limits_{i=1}^{500}\sum\limits_{j=1}^{500}([gcd(i,j)=1]a_i(tot_i-a_i))$, 反演一下可以$O(500log500)$计算.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 1e4+10, M = 510; int n, a[N], c[M]; vector<int> g[N]; int f[N][M], mu[M]; ll ans; void dfs(int x, int fa) { for (int y:g[x]) if (y!=fa) { dfs(y,x); REP(i,1,500) { f[x][i]+=f[y][i]; ll a = 0, b = 0; for (int j=i; j<=500; j+=i) a+=f[y][j],b+=(c[j]-f[y][j]); ans += mu[i]*a*b; } } ++f[x][a[x]]; } int main() { mu[1]=1; REP(i,1,500) for (int j=2*i;j<=500;j+=i) mu[j]-=mu[i]; scanf("%d", &n); REP(i,1,n) scanf("%d",a+i),++c[a[i]]; REP(i,2,n) { int u, v; scanf("%d%d", &u, &v); g[u].pb(v),g[v].pb(u); } dfs(1,0); printf("%lld\n", ans); }
困难难度就对每个因子建虚树, 最后容斥统计答案.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 1e5+10; int n, cnt, sum, vis[N], a[N], mu[N], mi[N], L[N], R[N]; int c[N], sz[N], dep[N], top[N], fa[N], son[N], s[N]; vector<int> g[N], f[N], v; ll ans; void dfs(int x, int d, int f) { L[x]=++*L,dep[x]=d,fa[x]=f; for (int y:g[x]) if (y!=f) { dfs(y,d+1,x),sz[x]+=sz[y]; if (sz[y]>sz[son[x]]) son[x]=y; } R[x]=*L; } void dfs(int x, int tf) { top[x]=tf; if (son[x]) dfs(son[x],tf); for (int y:g[x]) if (!top[y]) dfs(y,y); } int lca(int x, int y) { while (top[x]!=top[y]) { if (dep[top[x]]<dep[top[y]]) swap(x,y); x = fa[top[x]]; } return dep[x]<dep[y]?x:y; } void fac(int x, int num, int id) { if (x==v.size()) f[num].pb(id); else fac(x+1,num,id),fac(x+1,num*v[x],id); } bool cmp(int x, int y) {return L[x]<L[y];} void DP(int x, int z) { for (int y:g[x]) { DP(y,z); ans+=z*(ll)c[y]*(sum-c[y])*(dep[y]-dep[x]); c[x]+=c[y]; } c[x]+=vis[x]; } void solve(vector<int> a, int z) { for (int x:a) vis[x]=1; sum = a.size(); sort(a.begin(),a.end(),cmp); int sz = a.size(); REP(i,1,sz-1) a.pb(lca(a[i],a[i-1])); sort(a.begin(),a.end(),cmp); a.erase(unique(a.begin(),a.end()),a.end()); s[cnt=1]=a[0],sz=a.size(); REP(i,1,sz-1) { while (cnt>=1) { if (L[s[cnt]]<=L[a[i]]&&L[a[i]]<=R[s[cnt]]) { g[s[cnt]].pb(a[i]); break; } --cnt; } s[++cnt]=a[i]; } DP(s[1],z); for (int x:a) g[x].clear(),vis[x]=c[x]=0; } int main() { mu[1] = mi[1] = 1; REP(i,2,N-1) mi[i]=i,mu[i]=-1; REP(i,2,N-1) for(int j=2*i;j<N;j+=i) mu[j]-=mu[i],mi[j]=min(mi[j],i); scanf("%d", &n); REP(i,1,n) { int t, x; scanf("%d", &t); v.clear(); while (t!=1) { v.pb(x=mi[t]); while (t%x==0) t/=x; } fac(0,1,i); } REP(i,2,n) { int u, v; scanf("%d%d", &u, &v); g[u].pb(v),g[v].pb(u); } dfs(1,0,0),dfs(1,1); REP(i,1,n) g[i].clear(); REP(i,1,N-1) if (mu[i]&&f[i].size()) solve(f[i],mu[i]); printf("%lld\n", ans); }