Submission #30998243


Source Code Expand

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
using mint=modint998244353;

vector<int>A;  //A[i]=頂点iに書かれた値
vector<vector<int>>idx;  // idx[i]=値iが書かれた頂点たちからなるvector
vector<vector<int>>G;  // 隣接リスト
vector<int>visited;  // dfs時のフラグ
vector<mint>ans;  // ans[i]=パス上の頂点のgcdがiになるような頂点対の個数

void dfs(int v,int pre,int d,vector<int>&ans){
	//vを根とし、dの倍数のみが書かれた頂点からなる部分木Tを考え、
	//Tの各頂点について部分木の頂点数を求め、ansの末尾に追加する
	//ans.back()がvの部分木の頂点数になるようにする
	visited[v]=d;
	int cnt=1;
	for(auto vv:G[v])if(vv!=pre&&A[vv]%d==0){
		dfs(vv,v,d,ans);
		cnt+=ans.back();
	}
	ans.push_back(cnt);
}


int main(){
	int N;
	cin >> N;
	idx.resize(100010);
	A.resize(N);
	for(int i=0;i<N;i++){
		int a;
		cin >> a;
		idx[a].push_back(i);
		A[i]=a;
	}
	G.resize(N);
	for(int i=0;i<N-1;i++){
		int x,y;
		cin >> x >> y;
		G[x-1].push_back(y-1);
		G[y-1].push_back(x-1);
	}

	visited.resize(N);
	ans.resize(100010);
	mint kotae=0;
	for(int i=100000;i>0;i--){
		for(int d=i;d<=100000;d+=i){
			for(int v:idx[d])if(visited[v]!=i){
				vector<int>ret;
				dfs(v,-1,i,ret);
				mint num=ret.back();
				ret.pop_back();
				for(int x:ret)ans[i]+=(num-x)*x;
				ans[i]+=num*(num-1)/2;
			}
		}
		for(int d=2*i;d<=100000;d+=i)ans[i]-=ans[d];
		kotae+=ans[i]*i;
	}

	cout << kotae.val();

}

Submission Info

Submission Time
Task G - GCD cost on the tree
User kyopro_friends
Language C++ (GCC 9.2.1)
Score 600
Code Size 1603 Byte
Status AC
Exec Time 1861 ms
Memory 23640 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 2
AC × 39
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, hand_14.txt, hand_15.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt
Case Name Status Exec Time Memory
example_00.txt AC 18 ms 5748 KiB
example_01.txt AC 16 ms 5836 KiB
hand_00.txt AC 365 ms 13804 KiB
hand_01.txt AC 1338 ms 13728 KiB
hand_02.txt AC 1123 ms 13708 KiB
hand_03.txt AC 323 ms 16356 KiB
hand_04.txt AC 263 ms 15472 KiB
hand_05.txt AC 1043 ms 18416 KiB
hand_06.txt AC 1134 ms 20936 KiB
hand_07.txt AC 1619 ms 20276 KiB
hand_08.txt AC 1611 ms 20476 KiB
hand_09.txt AC 374 ms 23640 KiB
hand_10.txt AC 365 ms 20096 KiB
hand_11.txt AC 1272 ms 17380 KiB
hand_12.txt AC 1577 ms 17316 KiB
hand_13.txt AC 367 ms 21088 KiB
hand_14.txt AC 15 ms 5760 KiB
hand_15.txt AC 15 ms 5800 KiB
random_00.txt AC 1613 ms 20340 KiB
random_01.txt AC 364 ms 19224 KiB
random_02.txt AC 1047 ms 19620 KiB
random_03.txt AC 1036 ms 19696 KiB
random_04.txt AC 969 ms 20032 KiB
random_05.txt AC 984 ms 18520 KiB
random_06.txt AC 999 ms 20652 KiB
random_07.txt AC 1369 ms 14180 KiB
random_08.txt AC 324 ms 14792 KiB
random_09.txt AC 424 ms 13808 KiB
random_10.txt AC 1861 ms 13960 KiB
random_11.txt AC 404 ms 14220 KiB
random_12.txt AC 1759 ms 13768 KiB
random_13.txt AC 410 ms 14124 KiB
random_14.txt AC 1593 ms 13568 KiB
random_15.txt AC 348 ms 14352 KiB
random_16.txt AC 816 ms 13584 KiB
random_17.txt AC 690 ms 13632 KiB
random_18.txt AC 766 ms 13540 KiB
random_19.txt AC 656 ms 13504 KiB
random_20.txt AC 778 ms 13500 KiB