結果

問題 No.3250 最小公倍数
ユーザー kakel-san
提出日時 2025-08-29 22:16:54
言語 C#
(.NET 8.0.404)
結果
TLE  
実行時間 -
コード長 4,728 bytes
コンパイル時間 9,512 ms
コンパイル使用メモリ 171,120 KB
実行使用メモリ 318,572 KB
最終ジャッジ日時 2025-08-29 22:17:28
合計ジャッジ時間 30,058 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18 TLE * 3
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (120 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    static string[] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine()).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = NN;
        var a = NList;
        var map = NMap(n - 1);
        var tree = new List<int>[n];
        for (var i = 0; i < n; ++i) tree[i] = new List<int>();
        foreach (var edge in map)
        {
            tree[edge[0]].Add(edge[1]);
            tree[edge[1]].Add(edge[0]);
        }
        var minlist = GetMinPDivList(1_000_000);
        var ans = new long[n];
        DFS(0, -1, a, tree, minlist, ans);
        WriteLine(string.Join("\n", ans));
    }
    static int mod = 998_244_353;
    static Dictionary<int, int> DFS(int cur, int prev, int[] a, List<int>[] tree, int[] minlist, long[] ans)
    {
        var dic = PDiv(a[cur], minlist);
        ans[cur] = a[cur];
        foreach (var next in tree[cur])
        {
            if (next == prev) continue;
            var ndic = DFS(next, cur, a, tree, minlist, ans);
            if (dic.Count > ndic.Count)
            {
                foreach (var kv in ndic)
                {
                    if (dic.ContainsKey(kv.Key))
                    {
                        if (dic[kv.Key] < kv.Value)
                        {
                            ans[cur] = ans[cur] * Exp(kv.Key, kv.Value - dic[kv.Key]) % mod;
                            dic[kv.Key] = kv.Value;
                        }
                    }
                    else
                    {
                        ans[cur] = ans[cur] * Exp(kv.Key, kv.Value) % mod;
                        dic[kv.Key] = kv.Value;
                    }
                }
            }
            else
            {
                ans[cur] = ans[next];
                foreach (var kv in dic)
                {
                    if (ndic.ContainsKey(kv.Key))
                    {
                        if (ndic[kv.Key] < kv.Value)
                        {
                            ans[cur] = ans[cur] * Exp(kv.Key, kv.Value - ndic[kv.Key]) % mod;
                            ndic[kv.Key] = kv.Value;
                        }
                    }
                    else
                    {
                        ans[cur] = ans[cur] * Exp(kv.Key, kv.Value) % mod;
                        ndic[kv.Key] = kv.Value;
                    }
                }
                dic = ndic;
            }
        }
        return dic;
    }
    static void DebugDic<T, U>(Dictionary<T, U> dic)
    {
        Console.WriteLine("Dic:");
        foreach (var kv in dic) Console.WriteLine("  {0} = {1}", kv.Key, kv.Value);
    }
    static int[] GetMinPDivList(int max)
    {
        var minPDivList = new int[max + 1];
        for (var i = 0; i <= max; ++i) minPDivList[i] = i;

        for (var i = 2; i * i <= max; ++i) if (minPDivList[i] == i)
            {
                for (var j = i * i; j <= max; j += i) if (minPDivList[j] == j)
                    {
                        minPDivList[j] = i;
                    }
            }
        return minPDivList;
    }
    static int GetMinPDiv(int n, int min)
    {
        var p = min;
        while ((long)p * p <= n)
        {
            if (n % p == 0) return p;
            ++p;
        }
        return n;
    }
    static Dictionary<int, int> PDiv(int n, int[] minPDivList)
    {
        var p = minPDivList[n];
        if (n == p)
        {
            var dic = new Dictionary<int, int>();
            dic.Add(p, 1);
            return dic;
        }
        else
        {
            var dic = PDiv(n / p, minPDivList);
            if (dic.ContainsKey(p)) ++dic[p];
            else dic.Add(p, 1);
            return dic;
        }
    }
    static long Exp(long n, long p)
    {
        long _n = n % mod;
        var _p = p;
        var result = 1L;
        if ((_p & 1) == 1) result *= _n;
        while (_p > 0)
        {
            _n = _n * _n % mod;
            _p >>= 1;
            if ((_p & 1) == 1) result = result * _n % mod;
        }
        return result;
    }
}
0