結果

問題 No.2428 Returning Shuffle
ユーザー 👑 kakel-sankakel-san
提出日時 2023-08-18 21:55:03
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 833 ms / 2,000 ms
コード長 3,185 bytes
コンパイル時間 2,977 ms
コンパイル使用メモリ 66,480 KB
実行使用メモリ 103,964 KB
最終ジャッジ日時 2023-08-18 21:55:14
合計ジャッジ時間 10,186 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 468 ms
55,704 KB
testcase_01 AC 806 ms
61,032 KB
testcase_02 AC 833 ms
58,936 KB
testcase_03 AC 63 ms
21,844 KB
testcase_04 AC 64 ms
21,960 KB
testcase_05 AC 64 ms
21,712 KB
testcase_06 AC 64 ms
23,788 KB
testcase_07 AC 65 ms
21,848 KB
testcase_08 AC 66 ms
23,844 KB
testcase_09 AC 64 ms
21,708 KB
testcase_10 AC 64 ms
21,816 KB
testcase_11 AC 66 ms
21,732 KB
testcase_12 AC 63 ms
19,964 KB
testcase_13 AC 63 ms
21,908 KB
testcase_14 AC 64 ms
23,756 KB
testcase_15 AC 64 ms
23,940 KB
testcase_16 AC 65 ms
21,868 KB
testcase_17 AC 64 ms
21,796 KB
testcase_18 AC 65 ms
21,724 KB
testcase_19 AC 488 ms
46,744 KB
testcase_20 AC 476 ms
44,736 KB
testcase_21 AC 63 ms
21,932 KB
testcase_22 AC 61 ms
21,788 KB
testcase_23 AC 62 ms
21,756 KB
testcase_24 AC 527 ms
101,808 KB
testcase_25 AC 526 ms
103,964 KB
権限があれば一括ダウンロードができます

ソースコード

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();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var map = NArr(m);

        var p = new int[n];
        for (var i = 0; i < n; ++i) p[i] = i;
        foreach (var repl in map)
        {
            var tmp = p[repl[repl.Length - 1] - 1];
            for (var i = repl.Length - 1; i > 0; --i)
            {
                if (i > 1)
                {
                    p[repl[i] - 1] = p[repl[i - 1] - 1];
                }
                else
                {
                    p[repl[i] - 1] = tmp;
                }
            }
        }
        var uf = new UnionFindTree(n);
        for (var i = 0; i < n; ++i) uf.Unite(i, p[i]);
        var dic = new Dictionary<long, long>();
        for (var i = 0; i < n; ++i) if (uf.GetRoot(i) == i)
        {
            foreach (var kv in PDiv(uf.GetSize(i)))
            {
                if (dic.ContainsKey(kv.Key)) dic[kv.Key] = Math.Max(dic[kv.Key], kv.Value);
                else dic[kv.Key] = kv.Value;
            }
        }
        var ans = 1L;
        var mod = 998_244_353;
        foreach (var kv in dic) for (var i = 0; i < kv.Value; ++i) ans = ans * kv.Key % mod;
        WriteLine(ans);
    }
    class UnionFindTree
    {
        int[] roots;
        public UnionFindTree(int size)
        {
            roots = new int[size];
            for (var i = 0; i < size; ++i) roots[i] = -1;
        }
        public int GetRoot(int a)
        {
            if (roots[a] < 0) return a;
            return roots[a] = GetRoot(roots[a]);
        }
        public bool IsSameTree(int a, int b)
        {
            return GetRoot(a) == GetRoot(b);
        }
        public bool Unite(int a, int b)
        {
            var x = GetRoot(a);
            var y = GetRoot(b);
            if (x == y) return false;
            if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; }
            roots[x] += roots[y];
            roots[y] = x;
            return true;
        }
        public int GetSize(int a)
        {
            return -roots[GetRoot(a)];
        }
    }
    static Dictionary<long, long> PDiv(long n)
    {
        var dic = new Dictionary<long, long>();
        var tmp = n;
        while (tmp % 2 == 0)
        {
            tmp /= 2;
            if (dic.ContainsKey(2)) ++dic[2];
            else dic.Add(2, 1);
        }
        for (var p = 3L; p * p <= n; p += 2)
        {
            while (tmp % p == 0)
            {
                tmp /= p;
                if (dic.ContainsKey(p)) ++dic[p];
                else dic.Add(p, 1);
            }
            if (tmp == 1) break;
        }
        if (tmp > 1) dic.Add(tmp, 1);
        return dic;
    }
}
0