AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC393-E GCD of Subset
C#のソース
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5 2");
WillReturn.Add("3 4 6 7 12");
//3
//4
//6
//1
//6
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 3");
WillReturn.Add("6 10 15");
//1
//1
//1
}
else if (InputPattern == "Input3") {
WillReturn.Add("10 3");
WillReturn.Add("414003 854320 485570 52740 833292 625990 909680 885153 435420 221663");
//59
//590
//590
//879
//879
//590
//20
//879
//590
//59
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long K = wkArr[1];
long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long MaxA = AArr.Max();
// 個数[数]なDict
var CntDict = new Dictionary<long, long>();
foreach (long EachA in AArr) {
if (CntDict.ContainsKey(EachA) == false) {
CntDict[EachA] = 0;
}
CntDict[EachA]++;
}
// 答え[数]なDict
var AnswerDict = new Dictionary<long, long>();
foreach (long EachA in AArr) {
AnswerDict[EachA] = 1;
}
for (long I = 2; I <= MaxA; I++) {
var TargetList = new List<long>();
long AppearCnt = 0;
foreach (long EachBaisuu in DeriveBaisuuEnum(I, MaxA)) {
if (CntDict.ContainsKey(EachBaisuu)) {
TargetList.Add(EachBaisuu);
AppearCnt += CntDict[EachBaisuu];
}
}
if (AppearCnt >= K) {
TargetList.ForEach(pX => AnswerDict[pX] = I);
}
}
var sb = new System.Text.StringBuilder();
foreach (long EachA in AArr) {
sb.Append(AnswerDict[EachA]);
sb.AppendLine();
}
Console.Write(sb.ToString());
}
// 値と上限を引数として、倍数を列挙する
static IEnumerable<long> DeriveBaisuuEnum(long pVal, long pLimit)
{
for (long I = pVal; I <= pLimit; I += pVal) {
yield return I;
}
}
}
解説
重複値の対応で、度数分布表を求めてから、
調査級数の計算量で解いてます。