当前位置:网站首页>UNIQUE VISION Programming Contest 2022(AtCoder Beginner Contest 248)G. GCD cost on the tree
UNIQUE VISION Programming Contest 2022(AtCoder Beginner Contest 248)G. GCD cost on the tree
2022-07-28 17:10:00 【Code92007】
subject
n(n<=1e5) A tree at a point , Every point has a little power ai(1<=ai<=1e5),
For two points on the tree s、t, Definition C(s,t)=k×gcd(Ap1,Ap2,…,Apk),
among ,k by s To t The number of vertices of this chain ,p1 Is a point s,pk Is a point t, The middle point is the point on the chain
seek
, The answer is right 998244353 modulus
Source of ideas
dls/ Official explanation
Answer key 1( Inversion )
According to inversion , consider gcd The contribution of , Then count the multiples of each part's contribution ,
According to the formula
, take gcd(a1,...,an) The contribution of ,
That is, for each factor d, stay d On the path of multiples of , There are phi(d) The contribution of ,
Then the problem turns into , Build tree by factor ,dfs Trees , Contribute to each factor
TIPS
According to the formula
, Transposition to get
,
You can get O(nlogn) seek phi Methods , That is, initialization at the beginning phi(n)=n,
When enumerating to a certain pair ( factor , Multiple ) In a relationship , Subtract the factor from the multiple phi value
Code 1
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,M=1e5,mod=998244353;
int n,u,v,phi[N],a[N],sz[N],all,ans,cnt;
bool vis[N];
vector<int>e[N],fac[N],h[N];
vector<int>now;
struct edge{
int u,v;
}f[N];
void dfs(int u,int fa){
all++;
vis[u]=1;
sz[u]=1;
for(auto &v:e[u]){
if(v==fa)continue;
dfs(v,u);
sz[u]+=sz[v];
}
}
void dfs2(int u,int fa){
for(auto &v:e[u]){
if(v==fa)continue;
cnt=(cnt+1ll*sz[v]*(all-sz[v])%mod)%mod;
dfs2(v,u);
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
}
for(int i=1;i<=M;++i){
phi[i]=i;
}
for(int i=1;i<=M;++i){
fac[i].push_back(i);
for(int j=2*i;j<=M;j+=i){
fac[j].push_back(i);
phi[j]-=phi[i];
}
}
for(int i=1;i<n;++i){
scanf("%d%d",&u,&v);
f[i]=edge{u,v};
int g=__gcd(a[u],a[v]);
//printf("g:%d\n",g);
for(auto &w:fac[g]){
//printf("w:%d i:%d\n",w,i);
h[w].push_back(i);
}
}
for(int i=1;i<=M;++i){
if(!h[i].size())continue;
now.clear();
for(auto &w:h[i]){
u=f[w].u,v=f[w].v;
e[u].push_back(v);
e[v].push_back(u);
//printf("i:%d u:%d <- -> v:%d\n",i,u,v);
if(!vis[u]){
now.push_back(u);
vis[u]=1;
}
if(!vis[v]){
now.push_back(v);
vis[v]=1;
}
}
for(auto &v:now)vis[v]=0;
for(auto &v:now){
if(!vis[v]){
all=cnt=0;
dfs(v,-1);
dfs2(v,-1);
cnt=(cnt+1ll*all*(all-1)/2%mod)%mod;
//printf("i:%d phi:%d all:%d cnt:%d\n",i,phi[i],all,cnt);
ans=(ans+1ll*phi[i]*cnt%mod)%mod;
}
}
for(auto &v:now){
e[v].clear();
vis[v]=0;
}
}
printf("%d\n",ans);
return 0;
} Answer key 2( A class )
First, for each factor d, Find out d How many paths are there , Then because it will be heavy ,
remember ans[i] Is exactly the factor d The contribution of , be ans[i] Need to subtract ans[2*i],ans[3*i],..., Subtract the contribution of each multiple
Code 2
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,M=1e5,mod=998244353;
int n,u,v,phi[N],a[N],sz[N],all,ans[N],cnt,res;
bool vis[N];
vector<int>e[N],fac[N],h[N];
vector<int>now;
struct edge{
int u,v;
}f[N];
void dfs(int u,int fa){
all++;
vis[u]=1;
sz[u]=1;
for(auto &v:e[u]){
if(v==fa)continue;
dfs(v,u);
sz[u]+=sz[v];
}
}
void dfs2(int u,int fa){
for(auto &v:e[u]){
if(v==fa)continue;
cnt=(cnt+1ll*sz[v]*(all-sz[v])%mod)%mod;
dfs2(v,u);
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
}
for(int i=1;i<=M;++i){
phi[i]=i;
}
for(int i=1;i<=M;++i){
fac[i].push_back(i);
for(int j=2*i;j<=M;j+=i){
fac[j].push_back(i);
phi[j]-=phi[i];
}
}
for(int i=1;i<n;++i){
scanf("%d%d",&u,&v);
f[i]=edge{u,v};
int g=__gcd(a[u],a[v]);
//printf("g:%d\n",g);
for(auto &w:fac[g]){
//printf("w:%d i:%d\n",w,i);
h[w].push_back(i);
}
}
for(int i=1;i<=M;++i){
if(!h[i].size())continue;
now.clear();
for(auto &w:h[i]){
u=f[w].u,v=f[w].v;
e[u].push_back(v);
e[v].push_back(u);
//printf("i:%d u:%d <- -> v:%d\n",i,u,v);
if(!vis[u]){
now.push_back(u);
vis[u]=1;
}
if(!vis[v]){
now.push_back(v);
vis[v]=1;
}
}
for(auto &v:now)vis[v]=0;
for(auto &v:now){
if(!vis[v]){
all=cnt=0;
dfs(v,-1);
dfs2(v,-1);
cnt=(cnt+1ll*all*(all-1)/2%mod)%mod;
//printf("i:%d phi:%d all:%d cnt:%d\n",i,phi[i],all,cnt);
ans[i]=(ans[i]+cnt)%mod;
}
}
for(auto &v:now){
e[v].clear();
vis[v]=0;
}
}
for(int i=M;i;--i){
for(int j=2*i;j<=M;j+=i){
ans[i]=(ans[i]-ans[j]+mod)%mod;
}
res=(res+1ll*i*ans[i]%mod)%mod;
}
printf("%d\n",res);
return 0;
} 边栏推荐
- It is said that NVIDIA has held talks with Softbank and will offer more than US $32billion to acquire arm
- In 2020q2, shipments in the global tablet market soared by 26.1%: Huawei ranked third and Lenovo increased the most!
- HTAP是有代价的
- Codeforces round 770 (Div. 2) F. Fibonacci additions (construction + difference)
- Re11:读论文 EPM Legal Judgment Prediction via Event Extraction with Constraints
- Question making note 2 (add two numbers)
- 2020Q2全球平板市场出货大涨26.1%:华为排名第三,联想增幅最大!
- Question note 4 (the first wrong version, search the insertion position)
- Microsoft: edge browser has built-in disk cache compression technology, which can save space and not reduce system performance
- PostgreSQL weekly news - July 20, 2022
猜你喜欢

Ugui learning notes (II) Scrollview related

Games101 section 13 ray tracing notes

飞马D200S无人机与机载激光雷达在大比例尺DEM建设中的应用

College students participated in six Star Education PHP training and found jobs with salaries far higher than those of their peers
![[deep learning]: day 8 of pytorch introduction to project practice: weight decline (including source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 8 of pytorch introduction to project practice: weight decline (including source code)

Applet: scroll view slides to the bottom by default

Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making

Games101 assignment04 job 04

如何在构建阶段保护镜像安全

PostgreSQL weekly news - July 20, 2022
随机推荐
Re11:读论文 EPM Legal Judgment Prediction via Event Extraction with Constraints
打造自组/安全/可控的LoRa网!Semtech首度回应“工信部新规”影响
asmlinkage的理解
Unity shader uses rendered texture to achieve glass effect
2020Q2全球平板市场出货大涨26.1%:华为排名第三,联想增幅最大!
深入理解 DeepSea 和 Salt 部署工具 – Storage6
SUSE Ceph 增加节点、减少节点、 删除OSD磁盘等操作 – Storage6
Semtech推出物联网地理定位解决方案LoRa Edge,首款芯片LR1110现已上市
SUSE Storage6 环境搭建详细步骤 – Win10 + VMware WorkStation
Global mobile communication base station market in 2019: Ericsson, Huawei and Nokia ranked in the top three
SUSE Ceph 快速部署 – Storage6
Question making note 2 (add two numbers)
【深度学习】:《PyTorch入门到项目实战》第二天:从零实现线性回归(含详细代码)
SUSE CEPH add nodes, reduce nodes, delete OSD disks and other operations – storage6
Ugui learning notes (I) rendering level
Jsonarray traversal
传英伟达已与软银展开会谈,将出价超过320亿美元收购Arm
概率论与数理统计第一章
Realization of reflection and refraction effect in unity shader cube texture
Ugui learning notes (IV) ugui event system overview and Usage Summary