提出 #31088955


ソースコード 拡げる

#include <bits/stdc++.h>

template < typename T >
inline void read(T &cnt)
{
	cnt = 0; char ch = getchar(); bool op = 1;
	for (; ! isdigit(ch); ch = getchar())
		if (ch == '-') op = 0;
	for (; isdigit(ch); ch = getchar())
		cnt = cnt * 10 + ch - 48;
	cnt = op ? cnt : - cnt;
}
const int N = 1e5 + 10;
const int mod = 998244353;
int n, a[N], MAX;
int head[N], nxt[N * 2], to[N * 2], tot;

inline void add(int u, int v)
{
	nxt[++ tot] = head[u];
	head[u] = tot;
	to[tot] = v;
}

int vis[N], phi[N], prime[N], cnt;

bool cango[N], visit[N];

inline void Make_Prime()
{
	phi[1] = 1;
	for (int i = 2; i <= MAX; ++ i)
	{
		if (! vis[i])
		{
			vis[i] = i;
			prime[++ cnt] = i;
			phi[i] = i - 1;
		}

		for (int j = 1; j <= cnt; ++ j)
		{
			if (prime[j] * i > MAX || vis[i] < prime[j])
				break;
			vis[i * prime[j]] = prime[j];
			phi[i * prime[j]] = (i % prime[j]) ? (phi[i] * (prime[j] - 1)) : (phi[i] * prime[j]);
		}
	}
}

long long siz[N], dep[N], now, ans;
std::vector < int > V[N];

inline void dfs(int u, int fa)
{
	siz[u] = dep[u] = 1; visit[u] = true;
	for (int i = head[u]; i; i = nxt[i])
	{
		int v = to[i];
		if (v == fa || ! cango[v] || visit[v]) continue;
		dfs(v, u); 
		(now += dep[u] * siz[v] % mod + siz[u] * dep[v] % mod) %= mod;
		(dep[u] += dep[v] + siz[v]) %= mod;
		(siz[u] += siz[v]) %= mod;
	}
}

int main()
{
	read(n);

	for (int i = 1; i <= n; ++ i)
	{	
		read(a[i]); MAX = std::max(MAX, a[i]);
		V[a[i]].push_back(i);
	}
	Make_Prime();
	for (int i = 1; i < n; ++ i)
	{
		int u, v; read(u), read(v);
		add(u, v); add(v, u);
	}

	for (int i = 1; i <= MAX; ++ i)
	{
		now = 0;
		std::vector < int > go; go.clear();
		for (int j = i; j <= MAX; j += i)
			for (int k = 0; k < (int)V[j].size(); ++ k)
			{
				go.push_back(V[j][k]);
				cango[V[j][k]] = 1;
			}
		if (go.size() > 1) 
		{
			for (int j = 0; j < (int)go.size(); ++ j)
				if (! visit[go[j]])
					dfs(go[j], 0);	
		}
		for (int j = 0; j < (int)go.size(); ++ j)
			visit[go[j]] = 0, cango[go[j]] = 0;
		ans += now * phi[i] % mod;
		ans %= mod;
	}
	std::cout << (ans + mod) % mod << std::endl;
	return 0;
}

提出情報

提出日時
問題 G - GCD cost on the tree
ユーザ chzhc
言語 C++ (GCC 9.2.1)
得点 600
コード長 2196 Byte
結果 AC
実行時間 1067 ms
メモリ 22028 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 2
AC × 39
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
example_00.txt AC 19 ms 5864 KiB
example_01.txt AC 5 ms 5844 KiB
hand_00.txt AC 298 ms 11300 KiB
hand_01.txt AC 120 ms 11592 KiB
hand_02.txt AC 270 ms 11816 KiB
hand_03.txt AC 119 ms 14368 KiB
hand_04.txt AC 169 ms 12948 KiB
hand_05.txt AC 1037 ms 16296 KiB
hand_06.txt AC 721 ms 19140 KiB
hand_07.txt AC 424 ms 18856 KiB
hand_08.txt AC 419 ms 19096 KiB
hand_09.txt AC 108 ms 22028 KiB
hand_10.txt AC 333 ms 18212 KiB
hand_11.txt AC 866 ms 15408 KiB
hand_12.txt AC 444 ms 15672 KiB
hand_13.txt AC 333 ms 19020 KiB
hand_14.txt AC 5 ms 5880 KiB
hand_15.txt AC 12 ms 6572 KiB
random_00.txt AC 442 ms 18656 KiB
random_01.txt AC 92 ms 17576 KiB
random_02.txt AC 1008 ms 17772 KiB
random_03.txt AC 1067 ms 17556 KiB
random_04.txt AC 967 ms 18184 KiB
random_05.txt AC 999 ms 16524 KiB
random_06.txt AC 994 ms 18424 KiB
random_07.txt AC 409 ms 11760 KiB
random_08.txt AC 95 ms 12648 KiB
random_09.txt AC 535 ms 11300 KiB
random_10.txt AC 182 ms 11624 KiB
random_11.txt AC 507 ms 11148 KiB
random_12.txt AC 173 ms 11536 KiB
random_13.txt AC 514 ms 11572 KiB
random_14.txt AC 477 ms 11888 KiB
random_15.txt AC 99 ms 12648 KiB
random_16.txt AC 720 ms 11392 KiB
random_17.txt AC 707 ms 11552 KiB
random_18.txt AC 671 ms 11396 KiB
random_19.txt AC 660 ms 11292 KiB
random_20.txt AC 670 ms 11420 KiB