当前位置:网站首页>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;
} 边栏推荐
- Easypoi multi sheet export by template
- 2019年全球移动通信基站市场:爱立信、华为、诺基亚分列前三
- 2020Q2全球平板市场出货大涨26.1%:华为排名第三,联想增幅最大!
- 做题笔记2(两数相加)
- 海康威视回应'美国禁令'影响:目前所使用的元器件都有备选
- 累计出货130亿颗Flash,4亿颗MCU!深度解析兆易创新的三大产品线
- Understanding of asmlinkage
- The 16th program design competition of Dalian University of Technology (Problem Solver)
- 【深度学习】:《PyTorch入门到项目实战》第六天:多层感知机(含代码)
- asmlinkage的理解
猜你喜欢
![[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)

Unity shader transparent effect
![[deep learning]: day 4 of pytorch introduction to project practice: realize logistic regression from 0 to 1 (with source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 4 of pytorch introduction to project practice: realize logistic regression from 0 to 1 (with source code)
![[deep learning]: day 9 of pytorch introduction to project practice: dropout implementation (including source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 9 of pytorch introduction to project practice: dropout implementation (including source code)

【深度学习】:《PyTorch入门到项目实战》第七天之模型评估和选择(上):欠拟合和过拟合(含源码)

Ruoyi集成flyway后启动报错的解决方法

【深度学习】:《PyTorch入门到项目实战》第二天:从零实现线性回归(含详细代码)

MySQL安装教程

Easypoi --- excel file export

Re11: read EPM legal judgment prediction via event extraction with constraints
随机推荐
做题笔记3(二分查找)
概率论与数理统计第一章
Huawei mate 40 series exposure: large curvature hyperboloid screen, 5nm kylin 1020 processor! There will also be a version of Tianji 1000+
Time complexity
如何在构建阶段保护镜像安全
综合设计一个OPPE主页--页面的售后服务
Function接口之andThen
System clock failure of database fault tolerance
技术分享 | MySQL Shell 定制化部署 MySQL 实例
Codeforces round 770 (Div. 2) F. Fibonacci additions (construction + difference)
Unity shader global fog effect
Go language slow entry - process control statement
Question making note 3 (two point search)
数据库故障容错之系统时钟故障
Technology sharing | MySQL shell customized deployment MySQL instance
go语言慢速入门——流程控制语句
Semtech launched Lora edge, a geolocation solution for the Internet of things, and the first chip lr1110 is now on the market
Exercise note 5 (square of ordered array)
Some opinions on bug handling
充分利用----英文