結果
問題 |
No.2756 GCD Teleporter
|
ユーザー |
|
提出日時 | 2024-05-24 22:08:17 |
言語 | C# (.NET 8.0.404) |
結果 |
AC
|
実行時間 | 275 ms / 2,000 ms |
コード長 | 3,089 bytes |
コンパイル時間 | 9,053 ms |
コンパイル使用メモリ | 166,580 KB |
実行使用メモリ | 239,568 KB |
最終ジャッジ日時 | 2024-12-20 19:30:09 |
合計ジャッジ時間 | 17,267 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 36 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (106 ms)。 MSBuild のバージョン 17.9.6+a4ecab324 (.NET) main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
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 long[] LList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => long.Parse(ReadLine())).ToArray(); public static void Main() { Solve(); } static void Solve() { var n = NN; var a = NList; var uf = new UnionFindTree(n + 200001); var mlist = GetMinPDivList(200001); for (var i = 0; i < n; ++i) { foreach (var p in PDiv(a[i], mlist)) { uf.Unite(i, n + p); } } var visited = new HashSet<int>(); var list = new List<int>(); for (var i = 2 + n; i < n + 200001; ++i) { if (!visited.Contains(uf.GetRoot(i)) && uf.GetSize(i) > 1) { list.Add(i - n); visited.Add(uf.GetRoot(i)); } } var one = uf.GetSize(1 + n) - 1; int ans; if (list.Contains(2)) ans = 2 * (list.Count - 1 + one); else ans = 2 * (list.Count + one); if (list.Count > 0) ans = Math.Min(ans, list[0] * (list.Count - 1 + one)); WriteLine(ans); } 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 HashSet<int> PDiv(int n, int[] minPDivList) { var p = minPDivList[n]; if (n == p) { var dic = new HashSet<int>(); dic.Add(p); return dic; } else { var dic = PDiv(n / p, minPDivList); dic.Add(p); return dic; } } 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)]; } } }