虚树。
题目分析
直接做显然不好做。考虑拆贡献。注意到 ,于是考虑枚举质数 ,由于 时定有 ,则把含有质因数 的所有点拿出来建虚树。这是答案就是所有虚树上 之和。
再考虑如何计算 。发现其等于 ,证明显然。而一颗大小为 虚树上 的和就等于 ,可以维护。
于是就做完了,记总点数为 ,时间复杂度 ,瓶颈在建虚树。但由于 内数不同质因数个数最多为 ,所以可以取 。
AC Code
// by wangyizhi(571247)
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//bool Mst;
using namespace std;
namespace wyzfastio
{
//#define usefio
#ifdef usefio
namespace __getchar{const int bufsize=1<<20;char buf[bufsize<<1],*p1=buf,*p2=buf;inline char getchar(){return (p1==p2&&(p2=(p1=buf)+fread(buf,1,bufsize,stdin),p1==p2))?EOF:(*p1++);}}using __getchar::getchar;
namespace __putchar{const int bufsize=1<<20;char buf[bufsize<<1],*p=buf;inline void putchar(const char ch){if(p-buf==bufsize) fwrite(buf,1,bufsize,stdout),p=buf;*p++=ch;}inline void flush(){fwrite(buf,1,p-buf,stdout);}}using __putchar::putchar;using __putchar::flush;
#endif
/*---input---*/
inline void read(unsigned long long &x){char ch=getchar();unsigned long long res=0;while(ch<'0'||ch>'9')ch=getchar();while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res;}
inline void read(unsigned int &x){char ch=getchar();unsigned int res=0;while(ch<'0'||ch>'9')ch=getchar();while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res;}
inline void read(long long &x){char ch=getchar();long long f=1;unsigned long long res=0;while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res*f;}
inline void read(int &x){char ch=getchar();int f=1;unsigned res=0;while(ch<'0'||ch>'9'){if(ch=='-')f=-f;;ch=getchar();}while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res*f;}
inline void read(char &s){s=getchar();while(s==' '||s=='\n'||s=='\r') s=getchar();}
inline int read(char *s){int i=0;char ch=getchar();while(ch==' '||ch=='\n'||ch=='\r') ch=getchar();while(!(ch==' '||ch=='\n'||ch=='\r'||ch==EOF)) s[++i]=ch,ch=getchar();s[i+1]='\0';return i;}
inline void read(std::string &s){char ch=getchar();s.clear();while(ch==' '||ch=='\n'||ch=='\r') ch=getchar();while(!(ch==' '||ch=='\n'||ch=='\r'||ch==EOF)) s.push_back(ch),ch=getchar();}
template<typename _Tp,typename ...Args>inline void read(_Tp &x,Args &...args){read(x),read(args...);}
/*---output---*/
inline void write(const unsigned long long x){if(x<10) putchar(x+'0');else write(x/10),putchar(x%10+'0');}
inline void write(const long long x){unsigned long long t=x;if(x<0)putchar('-'),t=-x;write(t);}
inline void write(const unsigned int x){if(x<10) putchar(x+'0');else write(x/10),putchar(x%10+'0');}
inline void write(const int x){unsigned int t=x;if(x<0)putchar('-'),t=-x;write(t);}
inline void write(const char x){putchar(x);}
inline void write(const char *s){while(*s!='\0'&&*s!=EOF)putchar(*s),s++;}
template<typename _Tp,typename ...Args>inline void write(_Tp x,Args ...args){write(x),write(args...);}
}
using namespace wyzfastio;
using ll=long long;
using ld=long double;
//#define int ll
using pii=pair<int,int>;
const int N=2e5+5,mod=998244353,inv2=(mod+1)>>1;
inline int c2(int x){return 1ll*x*(x-1)/2%mod;}
int a[N];
int p[N],pcnt,to[N],mp[N];
inline void sieve(int n=2e5)
{
mp[1]=1;
for(int i=2;i<=n;i++)
{
if(!to[i]) p[++pcnt]=i,mp[i]=i;
for(int j=1;j<=pcnt&&i*p[j]<=n;j++)
{
to[i*p[j]]=1,mp[i*p[j]]=p[j];
if(!(i%p[j])) break;
}
}
}
vector<int> g[N],vec[N];
int dfn[N],dcnt,siz[N],dep[N],hson[N],fa[N],top[N];
void dfs1(int u)
{
siz[u]=1;
for(int v:g[u]) if(v!=fa[u])
{
dep[v]=dep[u]+1,fa[v]=u,dfs1(v),siz[u]+=siz[v];
if(!hson[u]||siz[hson[u]]<siz[v]) hson[u]=v;
}
}
void dfs2(int u,int tp)
{
dfn[u]=++dcnt,top[u]=tp;
if(!hson[u]) return;
dfs2(hson[u],tp);
for(int v:g[u]) if(v!=fa[u]&&v!=hson[u]) dfs2(v,v);
}
int lca(int u,int v)
{
while(top[u]!=top[v])
{
if(dep[top[u]]<dep[top[v]]) swap(u,v);
u=fa[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
return u;
}
vector<int> t[N];
int sz[N];
int dfscalc(int u)
{
int ans=0;
for(int v:t[u]) ans=(ans+dfscalc(v))%mod,ans=(ans+1ll*sz[u]*sz[v]%mod*dep[u])%mod,sz[u]+=sz[v];
return ans;
}
int solve(int x)
{
if(vec[x].size()<=2) return 0;
// write('>',x,'\n');
int ans=0,s=vec[x].size();
vector<int> vv=vec[x];
for(int u:vec[x]) sz[u]=1,ans=(ans+1ll*dep[u]*c2(s-1))%mod;
// write(ans,'\n');
sort(vv.begin(),vv.end(),[&](int x,int y){return dfn[x]<dfn[y];});
for(int i=1,l=vv.size();i<l;i++) vv.push_back(lca(vv[i-1],vv[i]));
vv.push_back(1),sort(vv.begin(),vv.end(),[&](int x,int y){return dfn[x]<dfn[y];});
vv.erase(unique(vv.begin(),vv.end()),vv.end());
for(int i=1;i<(int)vv.size();i++) t[lca(vv[i],vv[i-1])].push_back(vv[i]);
ans=(ans-1ll*dfscalc(1)*(s-2)%mod+mod)%mod;
// write(ans,'\n');
for(int u:vv) sz[u]=0,t[u].clear();
return ans;
}
//bool Med;
signed main()
{
// cerr<<"Memory Size: "<<abs((&Med)-(&Mst))/1024.0/1024<<" MB\n";
// ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
// freopen("in.in","r",stdin);
// freopen("out.out","w",stdout);
sieve();
int n,ans=0;
read(n);
for(int i=1,x;i<=n;i++)
{
read(x);
while(x>1)
{
int pp=mp[x];
vec[pp].push_back(i);
while(mp[x]==pp) x/=pp;
}
}
for(int i=1,u,v;i<n;i++) read(u,v),g[u].push_back(v),g[v].push_back(u);
dfs1(1),dfs2(1,1);
for(int i=1;i<=2e5;i++) ans=(ans+solve(i))%mod;
write(ans,'\n');
#ifdef usefio
flush();
#endif
return 0;
}